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 is any set of words, that is, any subset .
The language can be finite or infinite and need not follow a visible pattern.
Over :
-
, the words with an even number of ‘s:
contains and does not contain or .
-
, that is .
-
, the empty language, has no words. has exactly one word.
The complement of is the set of all words in that are not in :
Over , for example, the complement of (the words that start with ) is the set of words that are empty or start with .
Note that the complement depends on the alphabet.
Question 1. Answer each part before you open its answer.
(a) Is ?
Answer
Yes: take .
(b) Is ?
Answer
No: there is no with , because the number of ‘s in the word (two) differs from the number of ‘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 : the words that are in at least one of the two languages.
- Intersection : the words that are in both.
- Difference : the words that are in the first language and not in the second.
- Symmetric difference : the words that are in exactly one of the two.
- Complement : the words of that are not in .
In symbols, for every word :
In an expression without parentheses, intersection binds before union, like multiplication before addition: .
Example. Over let be the words that start with and the words that end with .
The figure sorts every word of length at most by the two questions.
- (inside both circles): , the words that start with and end with .
- (inside at least one circle): every word of the figure except the four outside: .
- (only in ): . A word that starts with and does not end with ends with .
- (inside exactly one circle): together with .
- (outside ): and , that is, and the words that start with .
Every list stops at length ; the languages themselves are infinite.
Check yourself. Answer each part before you open its answer.
(a) Describe in words and list its words of length at most .
Answer
The words that end with and do not start with .
A nonempty word that does not start with starts with , so these are the words that start and end with :
.
(b) Describe in words and list its words of length at most .
Answer
A word is outside the union exactly when it is outside both languages:
it does not start with and does not end with . These are , that is, and the words that start with and end with .
In symbols, .
(c) Prove that for all languages over .
Hint
Take an arbitrary word and rewrite "" step by step with the definitions above.
Each step should be an equivalence.
Proof
Let be any word. Then
The first equivalence is the definition of difference. The second is the definition of complement:
, so means . The third is the definition of intersection.
Since was an arbitrary word of and both languages are subsets of , the two languages contain exactly the same words.
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 (also written or ) holds all words made of a word of followed by a word of :
Here there are four names: report.txt, report.md, notes.txt and notes.md.
Question 3. With and , list .
Hint
Pair every word of with every word of . Some products may coincide, and a set lists each word once.
Worked answer
The four products are . As a set, .
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 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 is the alphabet of letters, digits and the underscore.
Answer
: the first letter from followed by any word, and no word of is empty.
Question 4. should mean ” words of in a row”.
For a word we defined . What should be, and how do you get from ?
The official definition
is a language with one word, the empty word, because choosing zero words from gives the empty word.
For the alphabet itself the same recursion gives and , so is the set of all words of length .
We see this by induction on : is the set of words of length ; and if is the set of words of length then , each such has length , and every word of length has the form with .
Every word has some length, which gives the exact form and the expanded form of :
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 . Which of belong to ? Which belong to , “at least one repetition”?
Hint
Write for this , and ask whether each word appears in one of them.
The official definitions
Here , and belong to ( does not), and and belong to , the positive closure of (the star without the choice of zero words).
Question 6. Answer each question, then open its answer.
(a) What are and ?
Answer
Both equal : a word of the product needs a word from each side, and has none.
(b) What are and ?
Answer
Both equal : attaching the empty word changes nothing.
So plays the role that plays in multiplication.
(c) What is ?
Answer
, because and every higher power is . So always contains .
(d) Does always hold?
Answer
No. For and we get and .
(e) Is always true?
Answer
For it is, but for a language it is not: if then as well.
Take : both and equal . The equality holds exactly when .
Reversal: .
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 :
| Words | |
|---|---|
| Prefixes | |
| Suffixes |
A substring of a word is any contiguous stretch of it, anywhere in it.
For :
| Words | |
|---|---|
| Substrings |
The word is not a substring of , because and are not adjacent in it.
Every prefix and every suffix is a substring, but not the other way round:
is a substring of that is neither a prefix nor a suffix.
For a language, is the language of the prefixes of words of , the language of their suffixes, and the language of all their substrings (including and the whole word):
Question 7. Let . Find each of these, then open its answer.
(1)
Answer
.
(2)
Answer
: the prefixes of and of .
(3)
Answer
: the suffixes of and of .
(4)
Answer
: all substrings of and of .
(5) Is for every language ?
Proof
Claim. For every language we have .
Proof. Let be any language and let be any word.
We show that . We must find such that .
- By the definition of , we have and there is with .
- Choose and ; both are in . Then , because for every word . This follows by induction on from the definition of concatenation: is its base case, and if then .
- By step 1, , so . By the definition of , therefore, .
Since was an arbitrary word of and an arbitrary language, the inclusion holds for every language.
Defining a language precisely: the natural numbers
Writing gives strings such as , 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 ? 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:
The thinking: every number is a string of digits.
The mistakes: strings such as belong to this language, and so does , which is not a number.
B:
The thinking: a first digit that is not zero, followed by any digits.
The mistakes: the number itself is missing, because every word of this language starts with a digit that is not zero.
C:
The thinking: any first digit, followed by any digits.
The mistakes: the first digit may be even when more digits follow, so belongs to this language.
It is the language of all nonempty strings of digits.
D:
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: and belong to it.
does not: it starts with but is not the single word .
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 . What must you show about in order to place it in ?
Hint
What does the definition of say about , and what does the definition of concatenation of languages say about the word that is reversed?
Answer
We must show that with and . From the side the inclusion starts from we take an arbitrary word and unfold it by the definitions:
with , where and . Then we show that and can be taken as the reverses of and of .
Example (proof). Claim: , where and . We prove both inclusions.
() Let . By the definition of there is with , and by the definition of concatenation of languages with and .
By Question 7 of Words: Inductive Definitions and Operations, .
Since , , and since , . Hence .
() Let . Then with and , so for some and for some .
By Question 7 of Words: Inductive Definitions and Operations, .
Since , we get .
Both inclusions hold, so the two languages are equal.
Example (refutation). is false: for and the left-hand side is and the right-hand side is .
The word 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:
- 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.
- Choose a description style. Set-builder notation with a condition () is clear for counting conditions. Exponent notation () is clear when the lengths of runs of the same letter are tied together, for example letters followed by letters . Concatenation and star of simple languages is clear for “phases” (a run of ‘s, then a run of ‘s).
- Translate between styles. but has no such description with only finitely many union, concatenation and star steps.
- Check the border cases. Is in the language? Does count?
Practice
1. List all words of the language:
Hint
There are four words of length over . Which of them contain at least one ?
Answer
.
2. Decide whether each claim is true or false, and read the reasoning.
(a)
True
The thinking: both are “empty” in some sense.
The mistake: has no words, while has exactly one word, the empty word.
They are different languages.
False
Reason: has zero words and has one.
(b)
True
Correct: and , so .
False
The thinking: has no words, so nothing can be built from it.
The mistake: the power takes no words at all, and gives whatever is.
(c) for every word and every
True
Correct: by the definition of the power and by (see Words: Inductive Definitions and Operations), and by induction on we get .
False
The thinking: perhaps the formula fails for .
The mistake: , so the formula holds for as well.
3. Describe in words, then list its words of length at most .
Hint
Each word is any word followed by . What do all of them share at the end?
Answer
All words that end with ; those of length at most are .
4. For find and the words of length at most in .
Hint
For pair every word of with every word of .
For write and keep the short words.
Answer
; the words of of length at most are .
5. (a) Refute: for all languages.
Refutation
Take and . Then whereas . The word is in the left-hand side and not in the right-hand side.
(b) Prove: for all languages.
Hint
Take a word on the left: it equals for some .
In which of the two languages is ?
Proof
Claim. For all languages we have .
Proof. We prove both inclusions.
() Let . By the definition of there is with .
By the definition of union, or . If , then by the definition of ; if , then by the definition of .
In both cases by the definition of union.
() Let . By the definition of union, or .
If , there is with ; since , also , so by the definition of .
If , there is with ; since , also , so by the definition of .
Since both inclusions hold, the two languages are equal.
More practice by levels, from easy questions to proofs and refutations, is on Languages: Practice and Check of Understanding, which closes this unit.