Exercise

Languages: Practice and Check of Understanding

Practice by levels on formal languages: membership and set operations by hand, a real-life DNA example, proofs of claims about languages, and refutations by counterexample.

Last updated:


On this practice page we practise deciding whether a word belongs to a language built with set operations; computing operations on small languages by hand; using languages in a real-life setting; proving claims about languages from the definitions; and refuting a false claim with one counterexample.

The questions start easy and get harder. Work on paper, and open a hint only after you have tried; open the solution only after you have written your own.
This practice follows Languages: Operations and Equalities, including the part on set operations.

Level 1: warm-up

Question 1 (DNA). A DNA sequence is a word over Σ={A,C,G,T}\Sigma=\{A,C,G,T\}⁠. A gene starts at a start codon, the three letters ATGATG⁠, and stops at a stop codon, one of the three-letter words TAATAA⁠, TAGTAG and TGATGA⁠.
Anyone searching a long sequence for genes needs a filter that marks the places that fit this pattern; a filter that is too simple also accepts sequences that are not genes, for example when the stop codon overlaps the start codon.
Define three languages over Σ\Sigma⁠:

Lstart={ATG⋅u∣u∈Σ∗}Lstop={u⋅s∣u∈Σ∗∧s∈{TAA,TAG,TGA}}Lcodons={w∈Σ∗∣∣w∣ mod 3=0}\begin{aligned} L_{\text{start}}&=\{ATG\cdot u\mid u\in\Sigma^*\}\\ L_{\text{stop}}&=\{u\cdot s\mid u\in\Sigma^*\wedge s\in\{TAA,TAG,TGA\}\}\\ L_{\text{codons}}&=\{w\in\Sigma^*\mid|w|\bmod 3=0\} \end{aligned}

In words: LstartL_{\text{start}} is the set of the words that start with a start codon, LstopL_{\text{stop}} the set of the words that end with a stop codon, and LcodonsL_{\text{codons}} the set of the words whose length is a multiple of 33⁠, that is, made of whole codons (a gene is read one codon, a group of three letters, at a time).
Take the words ATGTAAATGTAA⁠, ATGCCCTGAATGCCCTGA⁠, ATGAATGA⁠, ATGATG⁠, TAATAA and ε\varepsilon⁠.

Sort first. Drag every word to the column that fits it. There is a column for every combination of the three languages:
✓ means that the word is in the language, ✗ that it is not. Press “Check” to see how many words are in the right column.

The sorting tool needs JavaScript. Make the table on paper: one column for each combination of the three languages.

Now read your table to answer.

(a) Which of them belong to Lstart∩LstopL_{\text{start}}\cap L_{\text{stop}}⁠?

(b) Which belongs to Lstart−LstopL_{\text{start}}-L_{\text{stop}}⁠, and which to Lstop−LstartL_{\text{stop}}-L_{\text{start}}⁠?

(c) Which words belong to all three languages, and what does this say about ATGAATGA⁠?

Hint

For each word ask two separate questions, one for each language, and only then combine the answers with “and” or “and not”.

Solution

(a) ATGTAAATGTAA⁠, ATGCCCTGAATGCCCTGA and ATGAATGA⁠. The first two start with ATGATG and end with TAATAA and TGATGA⁠.
For ATGAATGA⁠, the last three letters are TGATGA⁠, a stop codon, although they overlap the start codon.
ATGATG does not end with a stop codon, TAATAA does not start with ATGATG⁠, and ε\varepsilon satisfies neither condition.

(b) ATGATG is in Lstart−LstopL_{\text{start}}-L_{\text{stop}}⁠: it starts with ATGATG and does not end with a stop codon.
TAATAA is in Lstop−LstartL_{\text{stop}}-L_{\text{start}}⁠.

(c) ATGTAAATGTAA (length 66⁠) and ATGCCCTGAATGCCCTGA (length 99⁠). The word ATGAATGA has length 44⁠, not a multiple of 33⁠:
its stop codon overlaps the start codon, so it cannot be read in whole codons.
The third language removes it, which is why a gene needs all three conditions.

Question 2. Let L1={ab,b}L_1=\{ab,b\} and L2={b,bb}L_2=\{b,bb\}⁠. Sort the words ab,b,bb,ba,εab, b, bb, ba, \varepsilon into the columns of the tool below.

The sorting tool needs JavaScript. Use a table of four columns on paper.

(a) Find L1∪L2L_1\cup L_2⁠, L1∩L2L_1\cap L_2⁠, L1−L2L_1-L_2 and L2−L1L_2-L_1⁠.

(b) Find the concatenation L1L2L_1L_2⁠.

(c) Find the reverse L1RL_1^R⁠.

Solution

(a) L1∪L2={ab,b,bb}L_1\cup L_2=\{ab,b,bb\}⁠, L1∩L2={b}L_1\cap L_2=\{b\}⁠, L1−L2={ab}L_1-L_2=\{ab\}⁠, L2−L1={bb}L_2-L_1=\{bb\}⁠.

(b) For L1L2L_1L_2 pair every word of L1L_1 with every word of L2L_2⁠:
ab⋅b=abbab\cdot b=abb⁠, ab⋅bb=abbbab\cdot bb=abbb⁠, b⋅b=bbb\cdot b=bb⁠, b⋅bb=bbbb\cdot bb=bbb⁠, so L1L2={abb,abbb,bb,bbb}L_1L_2=\{abb,abbb,bb,bbb\}⁠.

(c) Reversing every word, L1R={ba,b}L_1^R=\{ba,b\}⁠.

Question 3. Over Σ={a,b}\Sigma=\{a,b\} let L={w∈Σ∗∣#b(w)≥1}L=\{w\in\Sigma^*\mid\#_b(w)\geq 1\}⁠, the language of the words that contain the letter bb⁠.
Which of these is the complement LCL^C⁠? Decide before you open the options.

Candidate answers
A: the words that contain no aa

The thinking: this swaps the letters.

The mistakes: aaaa has no bb⁠, so it is in LCL^C⁠, but it contains an aa⁠, so A leaves it out.
A also includes bb and bbbb⁠, which contain the letter bb and so are in LL⁠, not in LCL^C⁠.
The complement depends only on the property “contains bb”.

B: the nonempty words that contain no bb

The thinking: the words without bb are a,aa,aaa,…a, aa, aaa,\ldots⁠, and the empty word is not considered.

The mistakes: the complement holds every word of Σ∗\Sigma^* that is not in LL⁠, and ε∈Σ∗\varepsilon\in\Sigma^* does not contain bb⁠, so ε∈LC\varepsilon\in L^C⁠.

C: the words that contain no bb

The thinking: a word is outside LL exactly when it does not contain bb⁠, so it is made only of aa‘s.
That includes the empty word, which has no bb⁠.

D: Σ∗\Sigma^*

The thinking: “everything that is not in LL”.

The mistakes: Σ∗\Sigma^* contains bb and abab⁠, which are in LL⁠, so they cannot be in LCL^C⁠.

Level 2: building, counting and describing

Question 4 (DNA, continued). With LstartL_{\text{start}} and LstopL_{\text{stop}} of Question 1:

(a) What is a shortest word in Lstart∩LstopL_{\text{start}}\cap L_{\text{stop}}⁠?

(b) How many words of length 44⁠, 55 and 66 does the intersection have?

Hint 1

What must hold for a word of the intersection: how does it start, and how does it end?

Hint 2

A word of the intersection starts with ATGATG⁠. Write its first three letters, then ask what the last three letters can be.
For a short word the first three and the last three letters overlap.

Solution

(a) Length 33 is impossible: the only word of length 33 that starts with ATGATG is ATGATG⁠, and it is not a stop codon.
A word of length 44 has the form ATGXATGX and its last three letters are TGXTGX⁠; this is a stop codon only for X=AX=A⁠.
So ATGAATGA is a shortest word.

(b) Length 44⁠: exactly one word, ATGAATGA⁠. Length 55⁠: the word is ATGXYATGXY and its last three letters GXYGXY start with GG⁠, but every stop codon starts with TT⁠, so there is no word.
Length 66⁠: the last three letters are the whole second half, and the first three are ATGATG⁠, so the word is ATGATG followed by one of TAATAA⁠, TAGTAG⁠, TGATGA⁠:
three words.

A real gene is read three letters at a time, so a word such as ATGAATGA would not be a gene.
The intersection also accepts words that are not genes, such as ATGAATGA⁠, so it is a necessary condition for a gene and not a sufficient one.

Question 5 (DNA, the language of genes). We define a language that describes genes better than the intersection of Question 4.
A candidate gene is a start codon, then any sequence of letters, then a stop codon, one after the other.
Let Sstop={TAA,TAG,TGA}S_{\text{stop}}=\{TAA,TAG,TGA\} be the set of the stop codons.

(a) Define the language LgeneL_{\text{gene}} of the candidate genes formally, using concatenation of languages.

Hint

Three parts come one after the other, and each part is a language:
the start codon, the middle, and the stop codon. Which language is the middle?

Solution Lgene={ATG}⋅Σ∗⋅Sstop={ATG⋅u⋅s∣u∈Σ∗∧s∈Sstop}L_{\text{gene}}=\{ATG\}\cdot\Sigma^*\cdot S_{\text{stop}}=\{ATG\cdot u\cdot s\mid u\in\Sigma^*\wedge s\in S_{\text{stop}}\}

Concatenation of words is associative, that is (xy)z=x(yz)(xy)z=x(yz) for all words (it is proved on this page, in Level 4, in the question on induction), so concatenation of languages is associative too and the parentheses can be left out.

(b) Prove that Lgene⊆Lstart∩LstopL_{\text{gene}}\subseteq L_{\text{start}}\cap L_{\text{stop}}⁠.

Hint

Take a word of LgeneL_{\text{gene}} and group it once as ATG⋅(u⋅s)ATG\cdot(u\cdot s) and once as (ATG⋅u)⋅s(ATG\cdot u)\cdot s⁠.

Proof

Let w∈Lgenew\in L_{\text{gene}}⁠. By the definition, w=ATG⋅u⋅sw=ATG\cdot u\cdot s with u∈Σ∗u\in\Sigma^* and s∈Sstops\in S_{\text{stop}}⁠.
Grouping as w=ATG⋅(u⋅s)w=ATG\cdot(u\cdot s) with u⋅s∈Σ∗u\cdot s\in\Sigma^* shows w∈Lstartw\in L_{\text{start}}⁠. Grouping as w=(ATG⋅u)⋅sw=(ATG\cdot u)\cdot s with ATG⋅u∈Σ∗ATG\cdot u\in\Sigma^* and s∈Sstops\in S_{\text{stop}} shows w∈Lstopw\in L_{\text{stop}}⁠.
The two groupings give the same word, because concatenation of words is associative.
■\blacksquare

(c) Find every word of (Lstart∩Lstop)−Lgene(L_{\text{start}}\cap L_{\text{stop}})-L_{\text{gene}}⁠.

Hint 1

For which lengths of a word can the stop codon overlap the start codon, as in ATGAATGA⁠?
The language LgeneL_{\text{gene}} does not allow it.

Hint 2

Split by length. For a word of length at least 66⁠, the first three and the last three letters do not overlap.
For shorter words use Question 4.

Solution

The difference is exactly {ATGA}\{ATGA\}⁠, so the inclusion of (b) is not an equality.
A word of LstartL_{\text{start}} has length at least 33⁠. By Question 4, the only word of length 33⁠, 44 or 55 in Lstart∩LstopL_{\text{start}}\cap L_{\text{stop}} is ATGAATGA⁠, and it is not in LgeneL_{\text{gene}}⁠, whose words have length at least 66⁠.
Now let w∈Lstart∩Lstopw\in L_{\text{start}}\cap L_{\text{stop}} with ∣w∣≥6|w|\geq 6⁠. Then w=ATG⋅vw=ATG\cdot v and w=x⋅sw=x\cdot s with s∈Sstops\in S_{\text{stop}} and ∣s∣=3|s|=3⁠.
Because ∣w∣≥6|w|\geq 6⁠, the first three and the last three letters of ww are disjoint, so w=ATG⋅u⋅sw=ATG\cdot u\cdot s where uu is what lies between them.
Hence w∈Lgenew\in L_{\text{gene}}⁠.

(d) Count the words of length 66 in each of the four regions:
only in LstartL_{\text{start}}⁠, in both LstartL_{\text{start}} and LstopL_{\text{stop}}⁠, only in LstopL_{\text{stop}}⁠, and in neither.

Hint

Count the words of length 66 in each language first: in LstartL_{\text{start}} the first three letters are fixed, in LstopL_{\text{stop}} the last three letters are one of three words.
The intersection is in Question 4. Then subtract.

Solution

There are 46=40964^6=4096 words of length 66⁠. In LstartL_{\text{start}} the first three letters are fixed and the other three are free:
43=644^3=64 words. In LstopL_{\text{stop}} the last three letters are one of 33 codons and the first three are free:
3⋅43=1923\cdot4^3=192 words. The intersection has 33 words (Question 4).
So: only LstartL_{\text{start}}⁠: 64−3=6164-3=61⁠; both: 33⁠; only LstopL_{\text{stop}}⁠: 192−3=189192-3=189⁠; neither:
4096−(61+3+189)=38434096-(61+3+189)=3843⁠.

(e) Challenge. A real gene is read in whole codons, so the part between the start and the stop codons must be made of codons too.
Let Σ3=Σ⋅Σ⋅Σ\Sigma^3=\Sigma\cdot\Sigma\cdot\Sigma be the set of all words of length 33 and Lorf={ATG}⋅(Σ3)∗⋅SstopL_{\text{orf}}=\{ATG\}\cdot(\Sigma^3)^*\cdot S_{\text{stop}} (“orf” is short for open reading frame: a start codon, whole codons, then a stop codon).
Prove that Lorf=Lgene∩LcodonsL_{\text{orf}}=L_{\text{gene}}\cap L_{\text{codons}}⁠, and say what this shows about ATGAATGA⁠.

Hint

For ⊇\supseteq use the length: ∣w∣=3+∣u∣+3|w|=3+|u|+3⁠. What does ∣w∣ mod 3=0|w|\bmod 3=0 say about ∣u∣|u|⁠?
Then cut uu into blocks of three letters.

Proof

(⊆\subseteq⁠) Let w∈Lorfw\in L_{\text{orf}}⁠, so w=ATG⋅u⋅sw=ATG\cdot u\cdot s with u∈(Σ3)∗u\in(\Sigma^3)^* and s∈Sstops\in S_{\text{stop}}⁠. Since (Σ3)∗⊆Σ∗(\Sigma^3)^*\subseteq\Sigma^*⁠, w∈Lgenew\in L_{\text{gene}}⁠.
Every word of Σ3\Sigma^3 has length 33⁠, so u∈(Σ3)ku\in(\Sigma^3)^k has length 3k3k for some kk⁠, and ∣w∣=3+3k+3=3(k+2)|w|=3+3k+3=3(k+2)⁠, a multiple of 33⁠.
So w∈Lcodonsw\in L_{\text{codons}}⁠.

(⊇\supseteq⁠) Let w∈Lgene∩Lcodonsw\in L_{\text{gene}}\cap L_{\text{codons}}⁠. Then w=ATG⋅u⋅sw=ATG\cdot u\cdot s with u∈Σ∗u\in\Sigma^* and s∈Sstops\in S_{\text{stop}}⁠, and ∣w∣=6+∣u∣|w|=6+|u| is a multiple of 33⁠, so ∣u∣=3k|u|=3k for some kk⁠.
Cutting off the first three letters of uu again and again (induction on kk⁠) writes uu as a concatenation of kk words of length 33⁠, so u∈(Σ3)k⊆(Σ3)∗u\in(\Sigma^3)^k\subseteq(\Sigma^3)^*⁠.
Hence w∈Lorfw\in L_{\text{orf}}⁠. ■\blacksquare

The word ATGAATGA has length 44⁠, not a multiple of 33⁠, so it is not in LcodonsL_{\text{codons}} and not in LorfL_{\text{orf}}⁠:
the three-letter reading frame removes the overlap that the intersection allowed.

Question 6 (file names, and a collision).

(a) Let B={report,notes}B=\{\texttt{report},\texttt{notes}\} be the language of the two base names and E={.txt,.md,.pdf}E=\{\texttt{.txt},\texttt{.md},\texttt{.pdf}\} the language of the three extensions.
How many words does BEBE have?

(b) Let L1={a,ab}L_1=\{a,ab\} and L2={bc,c}L_2=\{bc,c\}⁠. List L1L2L_1L_2⁠. Is the number of its words equal to the product ∣L1∣⋅∣L2∣|L_1|\cdot|L_2| of the numbers of words of L1L_1 and L2L_2⁠?
Explain.

Hint

For (b) write all four products, and look for two products that give the same word.

Solution

(a) Every base name can be followed by every extension, and no two pairs give the same word, so BEBE has 2⋅3=62\cdot3=6 words.

(b) The four products are a⋅bc=abca\cdot bc=abc⁠, a⋅c=aca\cdot c=ac⁠, ab⋅bc=abbcab\cdot bc=abbc and ab⋅c=abcab\cdot c=abc⁠, so L1L2={abc,ac,abbc}L_1L_2=\{abc,ac,abbc\}⁠:
three words, not four. The word abcabc is obtained in two ways, as a⋅bca\cdot bc and as ab⋅cab\cdot c⁠.
In general ∣L1L2∣≤∣L1∣⋅∣L2∣|L_1L_2|\leq|L_1|\cdot|L_2|⁠, with equality only when every word has one way to be split.

Level 3: refutations

To refute a claim “for all languages” one example is enough:
concrete languages, one concrete word, and a check that the word is on one side and not on the other.

Question 7. Four claims, for all languages L1,L2,L3L_1,L_2,L_3 over Σ\Sigma⁠:

(a) L1(L2∪L3)=L1L2∪L1L3L_1(L_2\cup L_3)=L_1L_2\cup L_1L_3⁠.

(b) (L1∩L2)L3=L1L3∩L2L3(L_1\cap L_2)L_3=L_1L_3\cap L_2L_3⁠.

(c) (L1∪L2)R=L1R∪L2R(L_1\cup L_2)^R=L_1^R\cup L_2^R⁠.

(d) (L1−L2)R=L1R−L2R(L_1-L_2)^R=L_1^R-L_2^R⁠.

Decide for each claim whether it is true, and prove or refute it.

Hint

Claim (a) is the distributive law of concatenation over union, which is proved in Level 4.
One of the claims is false, and it is about the interplay of intersection and concatenation.
Try languages for which L1∩L2L_1\cap L_2 is empty, and see what (L1∩L2)L3(L_1\cap L_2)L_3 is.

Solution

Claim (b) is false. Take L1={a}L_1=\{a\}⁠, L2={ab}L_2=\{ab\} and L3={b,ε}L_3=\{b,\varepsilon\}⁠. Then L1∩L2=∅L_1\cap L_2=\varnothing⁠, and no pair of a word of ∅\varnothing and a word of L3L_3 exists, so the left side (L1∩L2)L3=∅(L_1\cap L_2)L_3=\varnothing⁠.
On the right, L1L3={ab,a}L_1L_3=\{ab,a\} and L2L3={abb,ab}L_2L_3=\{abb,ab\}⁠, and abab belongs to both, so ab∈L1L3∩L2L3ab\in L_1L_3\cap L_2L_3⁠.
The word abab is in the right side and not in the left side, so the two sides differ.

The mistake behind the false claim is that a word may be split into a part from L1L_1 and a part from L3L_3 in one way, and into a part from L2L_2 and a part from L3L_3 in another way (a⋅ba\cdot b and ab⋅εab\cdot\varepsilon⁠).
The other three claims are true. (a) is the distributive law of concatenation over union, which is proved in Level 4.
(c) is proved in practice question 5 of Languages: Operations and Equalities.
(d): let ww be a word. If w∈(L1−L2)Rw\in(L_1-L_2)^R then w=vRw=v^R for some v∈L1v\in L_1 with v∉L2v\notin L_2⁠.
If we had w∈L2Rw\in L_2^R then w=uRw=u^R for some u∈L2u\in L_2⁠, and by the lemma (wR)R=w(w^R)^R=w (Question 13 in Level 4) v=(vR)R=(uR)R=u∈L2v=(v^R)^R=(u^R)^R=u\in L_2⁠, a contradiction; so w∈L1R−L2Rw\in L_1^R-L_2^R⁠.
Conversely, if w∈L1R−L2Rw\in L_1^R-L_2^R then w=vRw=v^R for some v∈L1v\in L_1⁠, and v∉L2v\notin L_2 because otherwise w∈L2Rw\in L_2^R⁠; so w∈(L1−L2)Rw\in(L_1-L_2)^R⁠.

Question 8. Refute each claim, for all languages over Σ={a,b}\Sigma=\{a,b\}⁠:

(a) L1L2=L2L1L_1L_2=L_2L_1⁠.

(b) (L∗)C=(LC)∗(L^*)^C=(L^C)^*⁠.

Hint

For (a) take two languages with one word each. For (b) take L={a}L=\{a\} and look for a word that is easy to place in both L∗L^* and LCL^C⁠.

Solution

(a) Take L1={a}L_1=\{a\} and L2={b}L_2=\{b\}⁠. Then L1L2={ab}L_1L_2=\{ab\} and L2L1={ba}L_2L_1=\{ba\}⁠, and ab≠baab\neq ba⁠.
Concatenation of languages is not commutative.

(b) Take L={a}L=\{a\} and consider the word aaaa⁠. Since aa=a⋅a∈L2⊆L∗aa=a\cdot a\in L^2\subseteq L^*⁠, we have aa∉(L∗)Caa\notin(L^*)^C⁠, so aaaa is not in the left side.
On the other hand aa≠aaa\neq a⁠, so aa∈LCaa\in L^C⁠, and LC=(LC)1⊆(LC)∗L^C=(L^C)^1\subseteq(L^C)^* gives aa∈(LC)∗aa\in(L^C)^*⁠.
The word aaaa is in the right side and not in the left side.

Level 4: proofs

A proof of equality of two languages shows both inclusions, or a chain of equivalences for an arbitrary word.
In every step name the definition that you use.

Question 9. Prove that (L1∪L2)C=L1C∩L2C(L_1\cup L_2)^C=L_1^C\cap L_2^C for all languages L1,L2L_1,L_2 over Σ\Sigma⁠.

Hint

Take an arbitrary w∈Σ∗w\in\Sigma^* and write a chain of equivalences that starts with "w∈(L1∪L2)Cw\in(L_1\cup L_2)^C".
The step from “not (A or B)” to “not A and not B” is a law of logic.

Proof

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

w∈(L1∪L2)C  ⟺  w∉L1∪L2definition of complement  ⟺  ¬ (w∈L1∨w∈L2)definition of union  ⟺  w∉L1∧w∉L2De Morgan’s law of logic  ⟺  w∈L1C∧w∈L2Cdefinition of complement, as w∈Σ∗  ⟺  w∈L1C∩L2Cdefinition of intersection.\begin{aligned} w\in(L_1\cup L_2)^C&\iff w\notin L_1\cup L_2&&\text{definition of complement}\\ &\iff \neg\,(w\in L_1\vee w\in L_2)&&\text{definition of union}\\ &\iff w\notin L_1\wedge w\notin L_2&&\text{De Morgan's law of logic}\\ &\iff w\in L_1^C\wedge w\in L_2^C&&\text{definition of complement, as }w\in\Sigma^*\\ &\iff w\in L_1^C\cap L_2^C&&\text{definition of intersection.} \end{aligned}

Every word of Σ∗\Sigma^* belongs to one side exactly when it belongs to the other, and both languages are subsets of Σ∗\Sigma^*⁠, so they are equal.
■\blacksquare

Question 10. Prove that L1(L2∪L3)=L1L2∪L1L3L_1(L_2\cup L_3)=L_1L_2\cup L_1L_3 for all languages L1,L2,L3L_1,L_2,L_3⁠. Recall that L1L2={xy∣x∈L1∧y∈L2}L_1L_2=\{xy\mid x\in L_1\wedge y\in L_2\}⁠.

Hint 1

What must you show in order to prove that two languages are equal?

Hint 2

Prove two inclusions. For ⊆\subseteq⁠, split into the cases y∈L2y\in L_2 and y∈L3y\in L_3⁠.
For ⊇\supseteq⁠, show that a word of L1L2L_1L_2 is also a word of the left side.

Proof

(⊆\subseteq⁠) Let w∈L1(L2∪L3)w\in L_1(L_2\cup L_3)⁠. By the definition of concatenation of languages, w=xyw=xy with x∈L1x\in L_1 and y∈L2∪L3y\in L_2\cup L_3⁠.
By the definition of union, y∈L2y\in L_2 or y∈L3y\in L_3⁠. If y∈L2y\in L_2⁠, then w=xy∈L1L2w=xy\in L_1L_2⁠; if y∈L3y\in L_3⁠, then w∈L1L3w\in L_1L_3⁠.
In both cases w∈L1L2∪L1L3w\in L_1L_2\cup L_1L_3⁠.

(⊇\supseteq⁠) Let w∈L1L2∪L1L3w\in L_1L_2\cup L_1L_3⁠. If w∈L1L2w\in L_1L_2⁠, then w=xyw=xy with x∈L1x\in L_1 and y∈L2y\in L_2⁠; since y∈L2y\in L_2⁠, also y∈L2∪L3y\in L_2\cup L_3⁠, so w∈L1(L2∪L3)w\in L_1(L_2\cup L_3)⁠.
If w∈L1L3w\in L_1L_3⁠, then w=xyw=xy with x∈L1x\in L_1 and y∈L3⊆L2∪L3y\in L_3\subseteq L_2\cup L_3⁠, so again w∈L1(L2∪L3)w\in L_1(L_2\cup L_3)⁠.
■\blacksquare

Question 11 (induction). Recall the definition of concatenation of words, by recursion on the second word:
x⋅ε=xx\cdot\varepsilon=x and x⋅(ua)=(x⋅u) ax\cdot(ua)=(x\cdot u)\,a for a word uu and a letter aa⁠. Prove that (xy)z=x(yz)(xy)z=x(yz) for all words x,y,zx,y,z⁠.

Hint

Fix xx and yy and use induction on zz⁠. Every word is ε\varepsilon or uaua⁠, so these two cases cover all words.
In the step, unfold both sides once with the second case of the definition, then use the induction hypothesis.

Proof

Fix words xx and yy⁠. We prove by induction on zz the statement P(z)P(z)⁠:
(xy)z=x(yz)(xy)z=x(yz)⁠.

Base case, z=εz=\varepsilon⁠. By the first case of the definition, (xy)ε=xy(xy)\varepsilon=xy and yε=yy\varepsilon=y⁠, so x(yε)=xyx(y\varepsilon)=xy⁠.
The two sides are equal.

Induction hypothesis. P(u)P(u) holds for a word uu⁠: (xy)u=x(yu)(xy)u=x(yu)⁠.

Induction step, z=uaz=ua⁠. By the second case of the definition with the first word xyxy⁠, (xy)(ua)=((xy)u) a(xy)(ua)=((xy)u)\,a⁠.
On the other side, y(ua)=(yu) ay(ua)=(yu)\,a by the second case, and then x((yu) a)=(x(yu)) ax\bigl((yu)\,a\bigr)=(x(yu))\,a by the second case again.
By the induction hypothesis (xy)u=x(yu)(xy)u=x(yu)⁠, so ((xy)u) a=(x(yu)) a((xy)u)\,a=(x(yu))\,a⁠, which is P(ua)P(ua)⁠.

Since every word is ε\varepsilon or has the form uaua⁠, P(z)P(z) holds for every word zz⁠.
■\blacksquare

Question 12. Prove that Pref(Pref(L))=Pref(L)\mathrm{Pref}(\mathrm{Pref}(L))=\mathrm{Pref}(L) for every language LL⁠, where

Pref(L)={x∈Σ∗∣∃y∈Σ∗ (xy∈L)}\mathrm{Pref}(L)=\{x\in\Sigma^*\mid\exists y\in\Sigma^*\ (xy\in L)\}

You may use that concatenation of words is associative: (xy)z=x(yz)(xy)z=x(yz) for all words.

Hint

For ⊇\supseteq choose y=εy=\varepsilon⁠. For ⊆\subseteq the definition gives two words in a row:
first a yy with xy∈Pref(L)xy\in\mathrm{Pref}(L)⁠, then a zz with (xy)z∈L(xy)z\in L⁠. Combine them into one word.

Proof

(⊇\supseteq⁠) Let x∈Pref(L)x\in\mathrm{Pref}(L)⁠. Choose y=ε∈Σ∗y=\varepsilon\in\Sigma^*⁠. Then xy=xε=x∈Pref(L)xy=x\varepsilon=x\in\mathrm{Pref}(L)⁠, so by the definition of Pref\mathrm{Pref} applied to the language Pref(L)\mathrm{Pref}(L)⁠, x∈Pref(Pref(L))x\in\mathrm{Pref}(\mathrm{Pref}(L))⁠.

(⊆\subseteq⁠) Let x∈Pref(Pref(L))x\in\mathrm{Pref}(\mathrm{Pref}(L))⁠. There is y∈Σ∗y\in\Sigma^* with xy∈Pref(L)xy\in\mathrm{Pref}(L)⁠, and so there is z∈Σ∗z\in\Sigma^* with (xy)z∈L(xy)z\in L⁠.
Because concatenation of words is associative, (xy)z=x(yz)(xy)z=x(yz)⁠, where yz∈Σ∗yz\in\Sigma^*⁠.
So u=yzu=yz is a word with xu∈Lxu\in L⁠, and x∈Pref(L)x\in\mathrm{Pref}(L) by the definition.
■\blacksquare

Question 13 (harder). Prove that (L1∩L2)R=L1R∩L2R(L_1\cap L_2)^R=L_1^R\cap L_2^R for all languages L1,L2L_1,L_2⁠.
Here LR={wR∣w∈L}L^R=\{w^R\mid w\in L\}⁠. You may use (uv)R=vRuR(uv)^R=v^Ru^R for words.

Hint 1

The inclusion ⊆\subseteq is a direct unfolding. For ⊇\supseteq you get two words x∈L1x\in L_1 and y∈L2y\in L_2 with xR=w=yRx^R=w=y^R⁠.
What must you show about xx and yy before you can say that a word is in L1∩L2L_1\cap L_2⁠?

Hint 2

First prove the lemma (wR)R=w(w^R)^R=w for every word ww⁠, by induction on ww using (ua)R=a uR(ua)^R=a\,u^R and (uv)R=vRuR(uv)^R=v^Ru^R⁠.
Then reverse both sides of xR=yRx^R=y^R⁠.

Proof

Lemma. (wR)R=w(w^R)^R=w for every word ww⁠. By induction on ww⁠.
For w=εw=\varepsilon⁠: εR=ε\varepsilon^R=\varepsilon⁠, so (εR)R=ε(\varepsilon^R)^R=\varepsilon⁠. Suppose (uR)R=u(u^R)^R=u and let w=uaw=ua⁠.
By the definition of reverse wR=a uRw^R=a\,u^R⁠, so (wR)R=(a uR)R=(uR)R aR(w^R)^R=(a\,u^R)^R=(u^R)^R\,a^R by (uv)R=vRuR(uv)^R=v^Ru^R⁠, and this is u a=wu\,a=w by the induction hypothesis and aR=aa^R=a⁠.

(⊆\subseteq⁠) Let w∈(L1∩L2)Rw\in(L_1\cap L_2)^R⁠. Then w=vRw=v^R with v∈L1∩L2v\in L_1\cap L_2⁠. Since v∈L1v\in L_1⁠, w∈L1Rw\in L_1^R⁠, and since v∈L2v\in L_2⁠, w∈L2Rw\in L_2^R⁠.
So w∈L1R∩L2Rw\in L_1^R\cap L_2^R⁠.

(⊇\supseteq⁠) Let w∈L1R∩L2Rw\in L_1^R\cap L_2^R⁠. Then w=xRw=x^R with x∈L1x\in L_1 and w=yRw=y^R with y∈L2y\in L_2⁠.
By the lemma, x=(xR)R=wR=(yR)R=yx=(x^R)^R=w^R=(y^R)^R=y⁠. So the word v=x=yv=x=y belongs to L1L_1 and to L2L_2⁠, that is, v∈L1∩L2v\in L_1\cap L_2⁠, and w=vR∈(L1∩L2)Rw=v^R\in(L_1\cap L_2)^R⁠.
■\blacksquare

The lemma shows that reversing loses no information, so two different words never have the same reverse.

Level 5: a challenge

Question 14. Decide whether each claim holds for all languages L1,L2L_1,L_2⁠.
Prove the true one and refute the false one.

(a) Pref(L1∪L2)=Pref(L1)∪Pref(L2)\mathrm{Pref}(L_1\cup L_2)=\mathrm{Pref}(L_1)\cup\mathrm{Pref}(L_2)⁠.

(b) Pref(L1∩L2)=Pref(L1)∩Pref(L2)\mathrm{Pref}(L_1\cap L_2)=\mathrm{Pref}(L_1)\cap\mathrm{Pref}(L_2)⁠.

For the false claim, say which of the two inclusions still holds, and prove it.

Hint

For (b) try L1={ab}L_1=\{ab\} and L2={ac}L_2=\{ac\}⁠: what is L1∩L2L_1\cap L_2⁠, and what are the prefixes of each language?

Solution

(a) is true. (⊆\subseteq⁠) Let x∈Pref(L1∪L2)x\in\mathrm{Pref}(L_1\cup L_2)⁠. There is yy with xy∈L1∪L2xy\in L_1\cup L_2⁠, so xy∈L1xy\in L_1 or xy∈L2xy\in L_2⁠.
In the first case x∈Pref(L1)x\in\mathrm{Pref}(L_1)⁠, in the second x∈Pref(L2)x\in\mathrm{Pref}(L_2)⁠, and in both cases xx is in the union.
(⊇\supseteq⁠) Let x∈Pref(L1)x\in\mathrm{Pref}(L_1)⁠: there is yy with xy∈L1⊆L1∪L2xy\in L_1\subseteq L_1\cup L_2⁠, so x∈Pref(L1∪L2)x\in\mathrm{Pref}(L_1\cup L_2)⁠; the same argument with L2L_2 in place of L1L_1 handles x∈Pref(L2)x\in\mathrm{Pref}(L_2)⁠.
■\blacksquare

(b) is false. Take L1={ab}L_1=\{ab\} and L2={ac}L_2=\{ac\}⁠. Then L1∩L2=∅L_1\cap L_2=\varnothing⁠, and Pref(∅)=∅\mathrm{Pref}(\varnothing)=\varnothing because no word extends to a word of the empty language.
But Pref(L1)={ε,a,ab}\mathrm{Pref}(L_1)=\{\varepsilon,a,ab\} and Pref(L2)={ε,a,ac}\mathrm{Pref}(L_2)=\{\varepsilon,a,ac\}⁠, whose intersection {ε,a}\{\varepsilon,a\} is not empty.
So the two sides differ.

The inclusion Pref(L1∩L2)⊆Pref(L1)∩Pref(L2)\mathrm{Pref}(L_1\cap L_2)\subseteq\mathrm{Pref}(L_1)\cap\mathrm{Pref}(L_2) does hold: if xy∈L1∩L2xy\in L_1\cap L_2 then xy∈L1xy\in L_1 and xy∈L2xy\in L_2⁠, so x∈Pref(L1)x\in\mathrm{Pref}(L_1) and x∈Pref(L2)x\in\mathrm{Pref}(L_2)⁠.
The converse fails because the two languages may extend xx in different ways.

See Deterministic Finite Automata: Deciding with Fixed Memory, on deciding whether a word belongs to a language.