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 gene starts at a start codon, the three letters , and stops at a stop codon, one of the three-letter words , and .
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 :
In words: is the set of the words that start with a start codon, the set of the words that end with a stop codon, and the set of the words whose length is a multiple of , that is, made of whole codons (a gene is read one codon, a group of three letters, at a time).
Take the words , , , , and .
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 ?
(b) Which belongs to , and which to ?
(c) Which words belong to all three languages, and what does this say about ?
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) , and . The first two start with and end with and .
For , the last three letters are , a stop codon, although they overlap the start codon.
does not end with a stop codon, does not start with , and satisfies neither condition.
(b) is in : it starts with and does not end with a stop codon.
is in .
(c) (length ) and (length ). The word has length , not a multiple of :
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 and . Sort the words into the columns of the tool below.
The sorting tool needs JavaScript. Use a table of four columns on paper.
(a) Find , , and .
(b) Find the concatenation .
(c) Find the reverse .
Solution
(a) , , , .
(b) For pair every word of with every word of :
, , , , so .
(c) Reversing every word, .
Question 3. Over let , the language of the words that contain the letter .
Which of these is the complement ? Decide before you open the options.
Candidate answers
A: the words that contain no
The thinking: this swaps the letters.
The mistakes: has no , so it is in , but it contains an , so A leaves it out.
A also includes and , which contain the letter and so are in , not in .
The complement depends only on the property “contains ”.
B: the nonempty words that contain no
The thinking: the words without are , and the empty word is not considered.
The mistakes: the complement holds every word of that is not in , and does not contain , so .
C: the words that contain no
The thinking: a word is outside exactly when it does not contain , so it is made only of ‘s.
That includes the empty word, which has no .
D:
The thinking: “everything that is not in ”.
The mistakes: contains and , which are in , so they cannot be in .
Level 2: building, counting and describing
Question 4 (DNA, continued). With and of Question 1:
(a) What is a shortest word in ?
(b) How many words of length , and 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 . 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 is impossible: the only word of length that starts with is , and it is not a stop codon.
A word of length has the form and its last three letters are ; this is a stop codon only for .
So is a shortest word.
(b) Length : exactly one word, . Length : the word is and its last three letters start with , but every stop codon starts with , so there is no word.
Length : the last three letters are the whole second half, and the first three are , so the word is followed by one of , , :
three words.
A real gene is read three letters at a time, so a word such as would not be a gene.
The intersection also accepts words that are not genes, such as , 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 be the set of the stop codons.
(a) Define the language 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
Concatenation of words is associative, that is 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 .
Hint
Take a word of and group it once as and once as .
Proof
Let . By the definition, with and .
Grouping as with shows . Grouping as with and shows .
The two groupings give the same word, because concatenation of words is associative.
(c) Find every word of .
Hint 1
For which lengths of a word can the stop codon overlap the start codon, as in ?
The language does not allow it.
Hint 2
Split by length. For a word of length at least , the first three and the last three letters do not overlap.
For shorter words use Question 4.
Solution
The difference is exactly , so the inclusion of (b) is not an equality.
A word of has length at least . By Question 4, the only word of length , or in is , and it is not in , whose words have length at least .
Now let with . Then and with and .
Because , the first three and the last three letters of are disjoint, so where is what lies between them.
Hence .
(d) Count the words of length in each of the four regions:
only in , in both and , only in , and in neither.
Hint
Count the words of length in each language first: in the first three letters are fixed, in the last three letters are one of three words.
The intersection is in Question 4. Then subtract.
Solution
There are words of length . In the first three letters are fixed and the other three are free:
words. In the last three letters are one of codons and the first three are free:
words. The intersection has words (Question 4).
So: only : ; both: ; only : ; neither:
.
(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 be the set of all words of length and (“orf” is short for open reading frame: a start codon, whole codons, then a stop codon).
Prove that , and say what this shows about .
Hint
For use the length: . What does say about ?
Then cut into blocks of three letters.
Proof
() Let , so with and . Since , .
Every word of has length , so has length for some , and , a multiple of .
So .
() Let . Then with and , and is a multiple of , so for some .
Cutting off the first three letters of again and again (induction on ) writes as a concatenation of words of length , so .
Hence .
The word has length , not a multiple of , so it is not in and not in :
the three-letter reading frame removes the overlap that the intersection allowed.
Question 6 (file names, and a collision).
(a) Let be the language of the two base names and the language of the three extensions.
How many words does have?
(b) Let and . List . Is the number of its words equal to the product of the numbers of words of and ?
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 has words.
(b) The four products are , , and , so :
three words, not four. The word is obtained in two ways, as and as .
In general , 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 over :
(a) .
(b) .
(c) .
(d) .
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 is empty, and see what is.
Solution
Claim (b) is false. Take , and . Then , and no pair of a word of and a word of exists, so the left side .
On the right, and , and belongs to both, so .
The word 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 and a part from in one way, and into a part from and a part from in another way ( and ).
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 be a word. If then for some with .
If we had then for some , and by the lemma (Question 13 in Level 4) , a contradiction; so .
Conversely, if then for some , and because otherwise ; so .
Question 8. Refute each claim, for all languages over :
(a) .
(b) .
Hint
For (a) take two languages with one word each. For (b) take and look for a word that is easy to place in both and .
Solution
(a) Take and . Then and , and .
Concatenation of languages is not commutative.
(b) Take and consider the word . Since , we have , so is not in the left side.
On the other hand , so , and gives .
The word 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 for all languages over .
Hint
Take an arbitrary and write a chain of equivalences that starts with "".
The step from “not (A or B)” to “not A and not B” is a law of logic.
Proof
Let be any word. Then
Every word of belongs to one side exactly when it belongs to the other, and both languages are subsets of , so they are equal.
Question 10. Prove that for all languages . Recall that .
Hint 1
What must you show in order to prove that two languages are equal?
Hint 2
Prove two inclusions. For , split into the cases and .
For , show that a word of is also a word of the left side.
Proof
() Let . By the definition of concatenation of languages, with and .
By the definition of union, or . If , then ; if , then .
In both cases .
() Let . If , then with and ; since , also , so .
If , then with and , so again .
Question 11 (induction). Recall the definition of concatenation of words, by recursion on the second word:
and for a word and a letter . Prove that for all words .
Hint
Fix and and use induction on . Every word is or , 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 and . We prove by induction on the statement :
.
Base case, . By the first case of the definition, and , so .
The two sides are equal.
Induction hypothesis. holds for a word : .
Induction step, . By the second case of the definition with the first word , .
On the other side, by the second case, and then by the second case again.
By the induction hypothesis , so , which is .
Since every word is or has the form , holds for every word .
Question 12. Prove that for every language , where
You may use that concatenation of words is associative: for all words.
Hint
For choose . For the definition gives two words in a row:
first a with , then a with . Combine them into one word.
Proof
() Let . Choose . Then , so by the definition of applied to the language , .
() Let . There is with , and so there is with .
Because concatenation of words is associative, , where .
So is a word with , and by the definition.
Question 13 (harder). Prove that for all languages .
Here . You may use for words.
Hint 1
The inclusion is a direct unfolding. For you get two words and with .
What must you show about and before you can say that a word is in ?
Hint 2
First prove the lemma for every word , by induction on using and .
Then reverse both sides of .
Proof
Lemma. for every word . By induction on .
For : , so . Suppose and let .
By the definition of reverse , so by , and this is by the induction hypothesis and .
() Let . Then with . Since , , and since , .
So .
() Let . Then with and with .
By the lemma, . So the word belongs to and to , that is, , and .
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 .
Prove the true one and refute the false one.
(a) .
(b) .
For the false claim, say which of the two inclusions still holds, and prove it.
Hint
For (b) try and : what is , and what are the prefixes of each language?
Solution
(a) is true. () Let . There is with , so or .
In the first case , in the second , and in both cases is in the union.
() Let : there is with , so ; the same argument with in place of handles .
(b) is false. Take and . Then , and because no word extends to a word of the empty language.
But and , whose intersection is not empty.
So the two sides differ.
The inclusion does hold: if then and , so and .
The converse fails because the two languages may extend in different ways.
See Deterministic Finite Automata: Deciding with Fixed Memory, on deciding whether a word belongs to a language.