Lesson

Languages: Operations and Equalities

Formal languages, the operations on them (complement, concatenation, powers, star, reverse, prefixes), and how to prove or refute an equality of languages.

Last updated:


What is a formal language, and how do we build languages from languages?

File names, autocomplete and text search all deal with collections of words:
a base name followed by an extension, beginnings of words, stretches inside a text.
Here we collect words into languages and operate on languages.
Words and the operations on them are defined in Words: Inductive Definitions and Operations.

A formal language over Σ\Sigma is any set of words, that is, any subset L⊆Σ∗L\subseteq\Sigma^*⁠.
The language can be finite or infinite and need not follow a visible pattern.
Over Σ={a,b}\Sigma=\{a,b\}⁠:

  • L1L_1⁠, the words with an even number of aa‘s:

    L1={w∈Σ∗∣#a(w) mod 2=0}L_1=\{w\in\Sigma^*\mid \#_a(w)\bmod 2=0\}

    L1L_1 contains ε,b,aa,abba\varepsilon, b, aa, abba and does not contain aa or babbab⁠.

  • L2={anbn∣n∈N}L_2=\{a^nb^n \mid n\in\mathbb{N}\}⁠, that is {ε,ab,aabb,aaabbb,…}\{\varepsilon, ab, aabb, aaabbb,\ldots\}⁠.

  • ∅\varnothing⁠, the empty language, has no words. {ε}\{\varepsilon\} has exactly one word.

The complement of LL is the set of all words in Σ∗\Sigma^* that are not in LL⁠:

LC=Σ∗−LL^C=\Sigma^*-L

Over Σ={a,b}\Sigma=\{a,b\}⁠, for example, the complement of {a⋅u∣u∈Σ∗}\{a\cdot u\mid u\in\Sigma^*\} (the words that start with aa⁠) is the set of words that are empty or start with bb⁠.
Note that the complement depends on the alphabet.

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

(a) Is ε∈L2\varepsilon\in L_2⁠?

Answer

Yes: take n=0n=0⁠.

(b) Is aab∈L2aab\in L_2⁠?

Answer

No: there is no nn with aab=anbnaab=a^nb^n⁠, because the number of aa‘s in the word (two) differs from the number of bb‘s (one).

Languages are sets, so set operations apply

A word either belongs to a language or it does not. Combining languages therefore comes down to asking, one word at a time, whether it is in the first language and whether it is in the second.
The set operations turn the everyday words “or”, “and” and “not” into operations on languages:

  • Union L1∪L2L_1\cup L_2⁠: the words that are in at least one of the two languages.
  • Intersection L1∩L2L_1\cap L_2⁠: the words that are in both.
  • Difference L1−L2L_1-L_2⁠: the words that are in the first language and not in the second.
  • Symmetric difference L1△L2L_1\triangle L_2⁠: the words that are in exactly one of the two.
  • Complement LCL^C⁠: the words of Σ∗\Sigma^* that are not in LL⁠.

In symbols, for every word w∈Σ∗w\in\Sigma^*⁠:

w∈L1∪L2  ⟺  w∈L1∨w∈L2w∈L1∩L2  ⟺  w∈L1∧w∈L2w∈L1−L2  ⟺  w∈L1∧w∉L2w∈L1△L2  ⟺  w∈(L1−L2)∪(L2−L1)w∈LC  ⟺  w∉L\begin{aligned} w\in L_1\cup L_2&\iff w\in L_1\vee w\in L_2\\ w\in L_1\cap L_2&\iff w\in L_1\wedge w\in L_2\\ w\in L_1-L_2&\iff w\in L_1\wedge w\notin L_2\\ w\in L_1\triangle L_2&\iff w\in(L_1-L_2)\cup(L_2-L_1)\\ w\in L^C&\iff w\notin L \end{aligned}

In an expression without parentheses, intersection binds before union, like multiplication before addition: L1∪L2∩L3=L1∪(L2∩L3)L_1\cup L_2\cap L_3=L_1\cup(L_2\cap L_3)⁠.

Example. Over Σ={a,b}\Sigma=\{a,b\} let L1L_1 be the words that start with aa and L2L_2 the words that end with bb⁠.
The figure sorts every word of length at most 33 by the two questions.

Two overlapping circles, L1 for words that start with a and L2 for words that end with b. Only in L1: a, aa, aaa, aba. In both: ab, aab, abb. Only in L2: b, bb, bab, bbb. Outside both circles: the empty word, ba, baa, bba.
  • L1∩L2L_1\cap L_2 (inside both circles): ab,aab,abbab, aab, abb⁠, the words that start with aa and end with bb⁠.
  • L1∪L2L_1\cup L_2 (inside at least one circle): every word of the figure except the four outside: ε,ba,baa,bba\varepsilon, ba, baa, bba⁠.
  • L1−L2L_1-L_2 (only in L1L_1⁠): a,aa,aaa,abaa, aa, aaa, aba⁠. A word that starts with aa and does not end with bb ends with aa⁠.
  • L1△L2L_1\triangle L_2 (inside exactly one circle): a,aa,aaa,abaa, aa, aaa, aba together with b,bb,bab,bbbb, bb, bab, bbb⁠.
  • L1CL_1^C (outside L1L_1⁠): b,bb,bab,bbbb, bb, bab, bbb and ε,ba,baa,bba\varepsilon, ba, baa, bba⁠, that is, ε\varepsilon and the words that start with bb⁠.

Every list stops at length 33⁠; the languages themselves are infinite.

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

(a) Describe L2−L1L_2-L_1 in words and list its words of length at most 33⁠.

Answer

The words that end with bb and do not start with aa⁠.
A nonempty word that does not start with aa starts with bb⁠, so these are the words that start and end with bb⁠:
b,bb,bab,bbbb, bb, bab, bbb⁠.

(b) Describe (L1∪L2)C(L_1\cup L_2)^C in words and list its words of length at most 33⁠.

Answer

A word is outside the union exactly when it is outside both languages:
it does not start with aa and does not end with bb⁠. These are ε,ba,baa,bba\varepsilon, ba, baa, bba⁠, that is, ε\varepsilon and the words that start with bb and end with aa⁠.
In symbols, (L1∪L2)C=L1C∩L2C(L_1\cup L_2)^C=L_1^C\cap L_2^C⁠.

(c) Prove that L1−L2=L1∩L2CL_1-L_2=L_1\cap L_2^C for all languages L1,L2L_1,L_2 over Σ\Sigma⁠.

Hint

Take an arbitrary word w∈Σ∗w\in\Sigma^* and rewrite "w∈L1−L2w\in L_1-L_2" step by step with the definitions above.
Each step should be an equivalence.

Proof

Let w∈Σ∗w\in\Sigma^* be any word. Then

w∈L1−L2  ⟺  w∈L1∧w∉L2  ⟺  w∈L1∧w∈L2C  ⟺  w∈L1∩L2C.w\in L_1-L_2\iff w\in L_1\wedge w\notin L_2\iff w\in L_1\wedge w\in L_2^C\iff w\in L_1\cap L_2^C.

The first equivalence is the definition of difference. The second is the definition of complement:
w∈Σ∗w\in\Sigma^*⁠, so w∉L2w\notin L_2 means w∈L2Cw\in L_2^C⁠. The third is the definition of intersection.
Since ww was an arbitrary word of Σ∗\Sigma^* and both languages are subsets of Σ∗\Sigma^*⁠, the two languages contain exactly the same words.
■\blacksquare

Operations that build languages from languages

The set operations above apply to every language. Four operations are special to words.

Concatenation of languages. Think of file names: a base name followed by an extension.

Question 2. The base names are report and notes, and the extensions are .txt and .md.
Which file names can you form, and how many are there? Formulate a rule that fits any two languages:
what is every word of the language formed by putting one language after the other?

Hint

Pair every base name with every extension. Does the order of the pair matter?

The official definition

The language L1⋅L2L_1\cdot L_2 (also written L1∘L2L_1\circ L_2 or L1L2L_1L_2⁠) holds all words made of a word of L1L_1 followed by a word of L2L_2⁠:

L1⋅L2={uv∣u∈L1∧v∈L2}L_1\cdot L_2=\{uv \mid u\in L_1\wedge v\in L_2\}

Here there are four names: report.txt, report.md, notes.txt and notes.md.

Question 3. With L1={a,ab}L_1=\{a,ab\} and L2={b,ε}L_2=\{b,\varepsilon\}⁠, list L1L2L_1L_2⁠.

Hint

Pair every word of L1L_1 with every word of L2L_2⁠. Some products may coincide, and a set lists each word once.

Worked answer

The four products are ab, a, abb, abab,\ a,\ abb,\ ab⁠. As a set, L1L2={a,ab,abb}L_1L_2=\{a,ab,abb\}⁠.

Back to the legal names. In Legal names as a language we wrote the language of legal names as a condition on the first letter.
Write it as a concatenation of two languages, where Let\mathrm{Let} is the language of the characters a name may start with:
the Latin letters and the underscore, without digits (every word in it has length 1) and Σ\Sigma is the alphabet of letters, digits and the underscore.

Answer

LP=Let⋅Σ∗L_P=\mathrm{Let}\cdot\Sigma^*⁠: the first letter from Let\mathrm{Let} followed by any word, and no word of Let⋅Σ∗\mathrm{Let}\cdot\Sigma^* is empty.

Question 4. LnL^n should mean ”nn words of LL in a row”.
For a word we defined w0=εw^0=\varepsilon⁠. What should L0L^0 be, and how do you get LnL^n from Ln−1L^{n-1}⁠?

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

L0L^0 is a language with one word, the empty word, because choosing zero words from LL gives the empty word.

For the alphabet itself the same recursion gives Σ0={ε}\Sigma^0=\{\varepsilon\} and Σn=Σn−1⋅Σ\Sigma^n=\Sigma^{n-1}\cdot\Sigma⁠, so Σn\Sigma^n is the set of all words of length nn⁠.
We see this by induction on nn⁠: Σ0={ε}\Sigma^0=\{\varepsilon\} is the set of words of length 00⁠; and if Σn−1\Sigma^{n-1} is the set of words of length n−1n-1 then Σn={ua∣u∈Σn−1∧a∈Σ}\Sigma^n=\{ua\mid u\in\Sigma^{n-1}\wedge a\in\Sigma\}⁠, each such uaua has length ∣u∣+1=n|u|+1=n⁠, and every word of length nn has the form uaua with ∣u∣=n−1|u|=n-1⁠.
Every word has some length, which gives the exact form and the expanded form of Σ∗\Sigma^*⁠:

Σ∗=⋃n=0∞Σn={ε}∪Σ∪Σ2∪Σ3∪⋯\begin{aligned} \Sigma^*&=\bigcup_{n=0}^{\infty}\Sigma^n\\ &=\{\varepsilon\}\cup\Sigma\cup\Sigma^2\cup\Sigma^3\cup\cdots \end{aligned}

Question 5. Search tools and form validators let you write a pattern such as (ab)*, which means “ab repeated any number of times, including none”.
This is the star operation, named after the logician Stephen Kleene.
Let L={ab}L=\{ab\}⁠. Which of ε, ab, abab, aba\varepsilon,\,ab,\,abab,\,aba belong to L∗L^*⁠? Which belong to L+L^+⁠, “at least one repetition”?

Hint

Write L0,L1,L2L^0,L^1,L^2 for this LL⁠, and ask whether each word appears in one of them.

The official definitions L∗=⋃n=0∞Ln={ε}∪L∪L2∪L3∪⋯L+=⋃n=1∞Ln=L∪L2∪L3∪⋯\begin{aligned} L^*&=\bigcup_{n=0}^{\infty}L^n\\ &=\{\varepsilon\}\cup L\cup L^2\cup L^3\cup\cdots\\ L^+&=\bigcup_{n=1}^{\infty}L^n\\ &=L\cup L^2\cup L^3\cup\cdots \end{aligned}

Here ε\varepsilon⁠, abab and abababab belong to L∗L^* (abaaba does not), and abab and abababab belong to L+L^+⁠, the positive closure of LL (the star without the choice of zero words).

Question 6. Answer each question, then open its answer.

(a) What are ∅⋅L\varnothing\cdot L and L⋅∅L\cdot\varnothing⁠?

Answer

Both equal ∅\varnothing⁠: a word of the product needs a word from each side, and ∅\varnothing has none.

(b) What are {ε}⋅L\{\varepsilon\}\cdot L and L⋅{ε}L\cdot\{\varepsilon\}⁠?

Answer

Both equal LL⁠: attaching the empty word changes nothing.
So {ε}\{\varepsilon\} plays the role that 11 plays in multiplication.

(c) What is ∅∗\varnothing^*⁠?

Answer

∅∗={ε}\varnothing^*=\{\varepsilon\}⁠, because ∅0={ε}\varnothing^0=\{\varepsilon\} and every higher power is ∅\varnothing⁠. So L∗L^* always contains ε\varepsilon⁠.

(d) Does L1L2=L2L1L_1L_2=L_2L_1 always hold?

Answer

No. For L1={a}L_1=\{a\} and L2={b}L_2=\{b\} we get L1L2={ab}L_1L_2=\{ab\} and L2L1={ba}L_2L_1=\{ba\}⁠.

(e) Is L+=L∗−{ε}L^+=L^*-\{\varepsilon\} always true?

Answer

For Σ+=Σ∗−{ε}\Sigma^+=\Sigma^*-\{\varepsilon\} it is, but for a language it is not: if ε∈L\varepsilon\in L then ε∈L+\varepsilon\in L^+ as well.
Take L={ε,a}L=\{\varepsilon,a\}⁠: both L∗L^* and L+L^+ equal {ε,a,aa,aaa,…}\{\varepsilon,a,aa,aaa,\ldots\}⁠. The equality holds exactly when ε∉L\varepsilon\notin L⁠.

Reversal: LR={wR∣w∈L}L^R=\{w^R \mid w\in L\}⁠.

Prefixes, suffixes and substrings. Autocomplete offers words that start with what you typed, a file extension such as .pdf is the end of a file name, and a search box looks for text anywhere inside a document.
These are a prefix, a suffix and a substring. A prefix of a word is a stretch of it that starts at its beginning, and a suffix is a stretch of it that ends at its end.
For abcabc⁠:

Words
Prefixesε, a, ab, abc\varepsilon,\ a,\ ab,\ abc
Suffixesε, c, bc, abc\varepsilon,\ c,\ bc,\ abc

A substring of a word is any contiguous stretch of it, anywhere in it.
For abcabc⁠:

Words
Substringsε, a, b, c, ab, bc, abc\varepsilon,\ a,\ b,\ c,\ ab,\ bc,\ abc

The word acac is not a substring of abcabc⁠, because aa and cc are not adjacent in it.
Every prefix and every suffix is a substring, but not the other way round:
bb is a substring of abcabc that is neither a prefix nor a suffix.

For a language, Pref(L)\mathrm{Pref}(L) is the language of the prefixes of words of LL⁠, Suff(L)\mathrm{Suff}(L) the language of their suffixes, and Sub(L)\mathrm{Sub}(L) the language of all their substrings (including ε\varepsilon and the whole word):

Pref(L)={w∈Σ∗∣∃y∈Σ∗ (wy∈L)}Suff(L)={w∈Σ∗∣∃x∈Σ∗ (xw∈L)}Sub(L)={w∈Σ∗∣∃x,z∈Σ∗ (xwz∈L)}\begin{aligned} \mathrm{Pref}(L)&=\{w\in\Sigma^*\mid\exists y\in\Sigma^*\ (wy\in L)\}\\ \mathrm{Suff}(L)&=\{w\in\Sigma^*\mid\exists x\in\Sigma^*\ (xw\in L)\}\\ \mathrm{Sub}(L)&=\{w\in\Sigma^*\mid\exists x,z\in\Sigma^*\ (xwz\in L)\} \end{aligned}

Question 7. Let L={abb,baa}L=\{abb,baa\}⁠. Find each of these, then open its answer.

(1) LRL^R

Answer

LR={bba,aab}L^R=\{bba,aab\}⁠.

(2) Pref(L)\mathrm{Pref}(L)

Answer

Pref(L)={ε,a,ab,abb,b,ba,baa}\mathrm{Pref}(L)=\{\varepsilon, a, ab, abb, b, ba, baa\}⁠: the prefixes of abbabb and of baabaa⁠.

(3) Suff(L)\mathrm{Suff}(L)

Answer

Suff(L)={ε,b,bb,abb,a,aa,baa}\mathrm{Suff}(L)=\{\varepsilon, b, bb, abb, a, aa, baa\}⁠: the suffixes of abbabb and of baabaa⁠.

(4) Sub(L)\mathrm{Sub}(L)

Answer

Sub(L)={ε,a,b,aa,ab,ba,bb,abb,baa}\mathrm{Sub}(L)=\{\varepsilon, a, b, aa, ab, ba, bb, abb, baa\}⁠: all substrings of abbabb and of baabaa⁠.

(5) Is Pref(L)⊆Sub(L)\mathrm{Pref}(L)\subseteq\mathrm{Sub}(L) for every language LL⁠?

Proof

Claim. For every language L⊆Σ∗L\subseteq\Sigma^* we have Pref(L)⊆Sub(L)\mathrm{Pref}(L)\subseteq\mathrm{Sub}(L)⁠.

Proof. Let LL be any language and let w∈Pref(L)w\in\mathrm{Pref}(L) be any word.
We show that w∈Sub(L)w\in\mathrm{Sub}(L)⁠. We must find x,z∈Σ∗x,z\in\Sigma^* such that xwz∈Lxwz\in L⁠.

  1. By the definition of Pref(L)\mathrm{Pref}(L)⁠, we have w∈Σ∗w\in\Sigma^* and there is y∈Σ∗y\in\Sigma^* with wy∈Lwy\in L⁠.
  2. Choose x=εx=\varepsilon and z=yz=y⁠; both are in Σ∗\Sigma^*⁠. Then xwz=(ε⋅w)⋅y=wyxwz=(\varepsilon\cdot w)\cdot y=wy⁠, because ε⋅w=w\varepsilon\cdot w=w for every word ww⁠. This follows by induction on ww from the definition of concatenation: ε⋅ε=ε\varepsilon\cdot\varepsilon=\varepsilon is its base case, and if ε⋅v=v\varepsilon\cdot v=v then ε⋅(va)=(ε⋅v) a=va\varepsilon\cdot(va)=(\varepsilon\cdot v)\,a=va⁠.
  3. By step 1, wy∈Lwy\in L⁠, so xwz∈Lxwz\in L⁠. By the definition of Sub(L)\mathrm{Sub}(L)⁠, therefore, w∈Sub(L)w\in\mathrm{Sub}(L)⁠.

Since ww was an arbitrary word of Pref(L)\mathrm{Pref}(L) and LL an arbitrary language, the inclusion holds for every language.
■\blacksquare

Defining a language precisely: the natural numbers

Writing {0,1,…,9}∗\{0,1,\ldots,9\}^* gives strings such as 007007⁠, which are not a standard way of writing a natural number.

Question 8. We want a language that contains exactly the natural numbers written in decimal, with no leading zero (except for the number zero itself).
Describe it with the operations of this lesson: union, concatenation and star.
This is a question to think about together in class: discuss it before you open anything below.

Hint 1

Which words may start with 00⁠? What may the first digit of every other word be?

Hint 2

Split the numbers into two separate cases: the number zero, and every other number.
Describe the second case as a first digit followed by any digits.

Only after your class has agreed on a description, open the candidates and choose the one you think is right:

Candidate descriptions
A: {0,1,…,9}∗\{0,1,\ldots,9\}^*

The thinking: every number is a string of digits.

The mistakes: strings such as 007007 belong to this language, and so does ε\varepsilon⁠, which is not a number.

B: {1,2,…,9}⋅{0,1,…,9}∗\{1,2,\ldots,9\}\cdot\{0,1,\ldots,9\}^*

The thinking: a first digit that is not zero, followed by any digits.

The mistakes: the number 00 itself is missing, because every word of this language starts with a digit that is not zero.

C: ({0}∪{1,2,…,9})⋅{0,1,…,9}∗\bigl(\{0\}\cup\{1,2,\ldots,9\}\bigr)\cdot\{0,1,\ldots,9\}^*

The thinking: any first digit, followed by any digits.

The mistakes: the first digit may be 00 even when more digits follow, so 007007 belongs to this language.
It is the language of all nonempty strings of digits.

D: {0} ∪ ({1,2,…,9}⋅{0,1,…,9}∗)\{0\}\ \cup\ \bigl(\{1,2,\ldots,9\}\cdot\{0,1,\ldots,9\}^*\bigr)

The thinking: two separate cases, the number zero and every other number, which starts with a digit that is not zero.

This is the description we want. Check it on four words: 00 and 1010 belong to it.
007007 does not: it starts with 00 but is not the single word 00⁠.
ε\varepsilon does not either, because both parts of the union need at least one letter.

Proving and refuting equalities of languages

To prove that two languages are equal, show both inclusions:
take an arbitrary word of the left-hand side of the equality and show it belongs to the right-hand side, then the other way around, each time using the definition of the operations.

Check yourself. Suppose w∈(L1⋅L2)Rw\in(L_1\cdot L_2)^R⁠. What must you show about ww in order to place it in L2R⋅L1RL_2^R\cdot L_1^R⁠?

Hint

What does the definition of LRL^R say about ww⁠, and what does the definition of concatenation of languages say about the word that is reversed?

Answer

We must show that w=pqw=pq with p∈L2Rp\in L_2^R and q∈L1Rq\in L_1^R⁠. From the side the inclusion starts from we take an arbitrary word and unfold it by the definitions:
w=vRw=v^R with v=xyv=xy⁠, where x∈L1x\in L_1 and y∈L2y\in L_2⁠. Then we show that pp and qq can be taken as the reverses of yy and of xx⁠.

Example (proof). Claim: (L1⋅L2)R=L2R⋅L1R(L_1\cdot L_2)^R=L_2^R\cdot L_1^R⁠, where LR={wR∣w∈L}L^R=\{w^R\mid w\in L\} and L1⋅L2={xy∣x∈L1∧y∈L2}L_1\cdot L_2=\{xy\mid x\in L_1\wedge y\in L_2\}⁠. We prove both inclusions.

(⊆\subseteq⁠) Let w∈(L1L2)Rw\in(L_1L_2)^R⁠. By the definition of LRL^R there is v∈L1L2v\in L_1L_2 with w=vRw=v^R⁠, and by the definition of concatenation of languages v=xyv=xy with x∈L1x\in L_1 and y∈L2y\in L_2⁠.
By Question 7 of Words: Inductive Definitions and Operations, w=(xy)R=yRxRw=(xy)^R=y^Rx^R⁠.
Since y∈L2y\in L_2⁠, yR∈L2Ry^R\in L_2^R⁠, and since x∈L1x\in L_1⁠, xR∈L1Rx^R\in L_1^R⁠. Hence w=yRxR∈L2RL1Rw=y^Rx^R\in L_2^RL_1^R⁠.

(⊇\supseteq⁠) Let w∈L2RL1Rw\in L_2^RL_1^R⁠. Then w=pqw=pq with p∈L2Rp\in L_2^R and q∈L1Rq\in L_1^R⁠, so p=yRp=y^R for some y∈L2y\in L_2 and q=xRq=x^R for some x∈L1x\in L_1⁠.
By Question 7 of Words: Inductive Definitions and Operations, w=yRxR=(xy)Rw=y^Rx^R=(xy)^R⁠.
Since xy∈L1L2xy\in L_1L_2⁠, we get w∈(L1L2)Rw\in(L_1L_2)^R⁠.

Both inclusions hold, so the two languages are equal.

Example (refutation). (L1L2)R=L1RL2R(L_1L_2)^R=L_1^RL_2^R is false: for L1={a}L_1=\{a\} and L2={b}L_2=\{b\} the left-hand side is {ba}\{ba\} and the right-hand side is {ab}\{ab\}⁠.
The word baba is in one side only.

Question 9. To refute a claimed equality of two languages, how many words do you need, and what must be true of them?

Answer

One word suffices: a word that belongs to one side of the equality and not to the other.
To prove an equality, one word is never enough: every word must be shown to belong to both sides.
Even a finite number of checks on particular languages, even thousands of them, does not prove a claim “for all languages”, because there are infinitely many languages.

Toolkit: reading and describing a language

Strategies for reading a language and describing it:

  1. List short words. Write the first few words in the language, and the first few that are just outside it, in order of length. A pattern appears quickly, and so does the shape of the definition.
  2. Choose a description style. Set-builder notation with a condition ({w∣#a(w) mod 2=0}\{w \mid \#_a(w)\bmod 2=0\}⁠) is clear for counting conditions. Exponent notation (anbna^nb^n⁠) is clear when the lengths of runs of the same letter are tied together, for example nn letters aa followed by nn letters bb⁠. Concatenation and star of simple languages is clear for “phases” (a run of aa‘s, then a run of bb‘s).
  3. Translate between styles. {aibj∣i,j≥0}={a}∗⋅{b}∗\{a^ib^j \mid i,j\geq0\}=\{a\}^*\cdot\{b\}^* but {anbn}\{a^nb^n\} has no such description with only finitely many union, concatenation and star steps.
  4. Check the border cases. Is ε\varepsilon in the language? Does n=0n=0 count?

Practice

1. List all words of the language:

L={w∈{a,b}∗∣∣w∣=2∧#a(w)≥1}L=\{w\in\{a,b\}^*\mid |w|=2 \wedge \#_a(w)\geq 1\}
Hint

There are four words of length 22 over {a,b}\{a,b\}⁠. Which of them contain at least one aa⁠?

Answer

aa, ab, baaa,\ ab,\ ba⁠.

2. Decide whether each claim is true or false, and read the reasoning.

(a) ∅={ε}\varnothing=\{\varepsilon\}

True

The thinking: both are “empty” in some sense.

The mistake: ∅\varnothing has no words, while {ε}\{\varepsilon\} has exactly one word, the empty word.
They are different languages.

False

Reason: ∅\varnothing has zero words and {ε}\{\varepsilon\} has one.

(b) ε∈∅∗\varepsilon\in\varnothing^*

True

Correct: ∅∗=⋃n≥0∅n\varnothing^*=\bigcup_{n\geq0}\varnothing^n and ∅0={ε}\varnothing^0=\{\varepsilon\}⁠, so ε∈∅∗\varepsilon\in\varnothing^*⁠.

False

The thinking: ∅\varnothing has no words, so nothing can be built from it.

The mistake: the power n=0n=0 takes no words at all, and gives {ε}\{\varepsilon\} whatever LL is.

(c) ∣wn∣=n⋅∣w∣|w^n|=n\cdot|w| for every word ww and every n∈Nn\in\mathbb{N}

True

Correct: ∣wn∣=∣wn−1⋅w∣=∣wn−1∣+∣w∣|w^n|=|w^{n-1}\cdot w|=|w^{n-1}|+|w| by the definition of the power and by ∣uv∣=∣u∣+∣v∣|uv|=|u|+|v| (see Words: Inductive Definitions and Operations), and by induction on nn we get n⋅∣w∣n\cdot|w|⁠.

False

The thinking: perhaps the formula fails for n=0n=0⁠.

The mistake: ∣w0∣=∣ε∣=0=0⋅∣w∣|w^0|=|\varepsilon|=0=0\cdot|w|⁠, so the formula holds for n=0n=0 as well.

3. Describe {a,b}∗⋅{ab}\{a,b\}^*\cdot\{ab\} in words, then list its words of length at most 33⁠.

Hint

Each word is any word followed by abab⁠. What do all of them share at the end?

Answer

All words that end with abab⁠; those of length at most 33 are ab, aab, babab,\ aab,\ bab⁠.

4. For L={ab,b}L=\{ab,b\} find L2L^2 and the words of length at most 33 in L∗L^*⁠.

Hint

For L2L^2 pair every word of LL with every word of LL⁠.
For L∗L^* write L0,L1,L2,L3L^0,L^1,L^2,L^3 and keep the short words.

Answer

L2={abab, abb, bab, bb}L^2=\{abab,\ abb,\ bab,\ bb\}⁠; the words of L∗L^* of length at most 33 are ε, b, ab, bb, abb, bab, bbb\varepsilon,\ b,\ ab,\ bb,\ abb,\ bab,\ bbb⁠.

5. (a) Refute: (L1∪L2)R=L1R∩L2R(L_1\cup L_2)^R=L_1^R\cap L_2^R for all languages.

Refutation

Take L1={a}L_1=\{a\} and L2={b}L_2=\{b\}⁠. Then (L1∪L2)R={a,b}(L_1\cup L_2)^R=\{a,b\} whereas L1R∩L2R={a}∩{b}=∅L_1^R\cap L_2^R=\{a\}\cap\{b\}=\varnothing⁠. The word aa is in the left-hand side and not in the right-hand side.

(b) Prove: (L1∪L2)R=L1R∪L2R(L_1\cup L_2)^R=L_1^R\cup L_2^R for all languages.

Hint

Take a word ww on the left: it equals vRv^R for some v∈L1∪L2v\in L_1\cup L_2⁠.
In which of the two languages is vv⁠?

Proof

Claim. For all languages L1,L2L_1,L_2 we have (L1∪L2)R=L1R∪L2R(L_1\cup L_2)^R=L_1^R\cup L_2^R⁠.

Proof. We prove both inclusions.

(⊆\subseteq⁠) Let w∈(L1∪L2)Rw\in(L_1\cup L_2)^R⁠. By the definition of LRL^R there is v∈L1∪L2v\in L_1\cup L_2 with w=vRw=v^R⁠.
By the definition of union, v∈L1v\in L_1 or v∈L2v\in L_2⁠. If v∈L1v\in L_1⁠, then w=vR∈L1Rw=v^R\in L_1^R by the definition of L1RL_1^R⁠; if v∈L2v\in L_2⁠, then w=vR∈L2Rw=v^R\in L_2^R by the definition of L2RL_2^R⁠.
In both cases w∈L1R∪L2Rw\in L_1^R\cup L_2^R by the definition of union.

(⊇\supseteq⁠) Let w∈L1R∪L2Rw\in L_1^R\cup L_2^R⁠. By the definition of union, w∈L1Rw\in L_1^R or w∈L2Rw\in L_2^R⁠.
If w∈L1Rw\in L_1^R⁠, there is v∈L1v\in L_1 with w=vRw=v^R⁠; since v∈L1v\in L_1⁠, also v∈L1∪L2v\in L_1\cup L_2⁠, so w=vR∈(L1∪L2)Rw=v^R\in(L_1\cup L_2)^R by the definition of LRL^R⁠.
If w∈L2Rw\in L_2^R⁠, there is v∈L2v\in L_2 with w=vRw=v^R⁠; since v∈L2v\in L_2⁠, also v∈L1∪L2v\in L_1\cup L_2⁠, so w=vR∈(L1∪L2)Rw=v^R\in(L_1\cup L_2)^R by the definition of LRL^R⁠.

Since both inclusions hold, the two languages are equal. ■\blacksquare

More practice by levels, from easy questions to proofs and refutations, is on Languages: Practice and Check of Understanding, which closes this unit.