Exercise

Describe the Language with Operations

Practice describing languages with union, intersection, difference, complement, concatenation, star, reverse and prefixes: requirements from DNA, codes and file names turned into expressions and checked exactly, three representations of one language, and an open lab.

Last updated:


From a requirement in words to an expression

A requirement such as “a DNA sequence that starts with ATG and ends with a stop codon” is a condition on words.
Each part of it, “starts with ATG”, “ends with a stop codon”, is a language, and “and” combines the two.
The operations on languages (union, intersection, difference, complement, concatenation, star, reverse, prefixes) combine languages into a new language.
An expression is a combination of languages by means of these operations, and two expressions are equal when they describe exactly the same words.
This requirement is solved in Languages: Practice and Check of Understanding, in questions 4 and 5.

The tool decides equality exactly, and not by trying words. When your expression is not the required language, the tool shows a shortest word on which they differ and which side contains it.
Questions are drawn at random; press “New task” for another one.
The tool has three activities, among them the open lab: in it you choose reference languages (they are named L1L_1⁠, L2L_2⁠, …), write an expression over them and see its words.

The tool needs JavaScript. The written questions below can be done on paper.

Written questions

Question 1. Let L1L_1 be the words over {a,b}\{a,b\} that start with aa⁠, L2L_2 the words that end with bb⁠, and L3L_3 the words of even length.
The expressions L1∪L2∩L3L_1\cup L_2\cap L_3 and (L1∪L2)∩L3(L_1\cup L_2)\cap L_3 look alike. In the open lab choose L1L_1⁠, L2L_2 and L3L_3⁠, and compare them.
On which word do they differ, and which of the two contains it?

Hint

By the order of operations in Languages: Operations and Equalities, intersection binds before union, so the first expression is L1∪(L2∩L3)L_1\cup(L_2\cap L_3)⁠.
Check the words of length 00⁠, 11⁠, 22 in this order.

Answer

The shortest word is aa⁠. It starts with aa⁠, so it is in L1L_1 and therefore in L1∪(L2∩L3)L_1\cup(L_2\cap L_3)⁠.
In (L1∪L2)∩L3(L_1\cup L_2)\cap L_3 it is not, because its length 11 is odd, so it is not in L3L_3⁠.
The words ε\varepsilon and bb are in neither expression, so aa is the shortest difference.

Question 2. Over the alphabet {a,b,0,1}\{a,b,0,1\} a code is a word. With Let={a,b}\mathrm{Let}=\{a,b\} (the letters, without the digits), Dig={0,1}\mathrm{Dig}=\{0,1\} and Any=Σ∗\mathrm{Any}=\Sigma^*⁠, write an expression for “a code that starts with a letter and ends with a digit”.
Do you need to add a condition on the length of the code to the expression?
Explain.

Hint

Try a code of length 11⁠.

Answer

Let⋅Any⋅Dig\mathrm{Let}\cdot\mathrm{Any}\cdot\mathrm{Dig}⁠. The condition on the length already follows from the expression:
a word of this language has a first character from Let\mathrm{Let} and a last character from Dig\mathrm{Dig}⁠, and these are two different characters, because no character is both a letter and a digit.
So every word of the language has at least two characters, and the middle part Any\mathrm{Any} may be empty.

Question 3. Show that (L1∩L2)C=L1C∪L2C(L_1\cap L_2)^C=L_1^C\cup L_2^C for all languages L1,L2L_1,L_2 over the same alphabet, after checking it in the open lab on two reference languages.

Hint

Take a word w∈Σ∗w\in\Sigma^* and unfold ”ww is in the complement of an intersection” with the definitions of complement and intersection.

Answer

For w∈Σ∗w\in\Sigma^*⁠:

w∈(L1∩L2)C  ⟺  w∉L1∩L2  ⟺  ¬(w∈L1 and w∈L2)  ⟺  w∉L1 or w∉L2  ⟺  w∈L1C∪L2Cw\in(L_1\cap L_2)^C \iff w\notin L_1\cap L_2 \iff \neg(w\in L_1\ \text{and}\ w\in L_2) \iff w\notin L_1\ \text{or}\ w\notin L_2 \iff w\in L_1^C\cup L_2^C

The two languages have the same words, so they are equal.