Words: Inductive Definitions and Operations
Words defined inductively (length, letter count, concatenation, powers, reverse), and why every yes/no question about text is a question about a language.
Last updated:
Which texts are legal names?
A compiler (the tool that translates a program into machine instructions) receives the text total_2 = 7.
Before it can translate anything, it must answer a much simpler question:
is total_2 a legal name? Is 2total one? A login form asks the same kind of question about a password, and a search box about a date.
If the check is wrong, the compiler accepts a faulty program or rejects a correct one, and the form rejects a valid password or accepts one that breaks the rules.
Try it yourself: Python checks names before it runs a program.
Run the cell, then change the name total_2 (in both lines) to 2total, to total-2 and to class.
Before each run, predict whether Python will accept the name.
total_2 = 7
print(total_2)
7
For each name Python gives one of two answers: yes, the name is legal and the program runs, or no, it stops with a SyntaxError.
Decision problems
Each of these questions is a yes/no question about a piece of text, and such a question is called a decision problem.
The goal of this course is to build computational models, that is, machines defined precisely by mathematical definitions, so that we can characterize which problems a computer can solve.
A decision problem is given by a predicate: a statement about a word that is either true or false, for example = ” is a legal name”.
Let be the language of all the words that satisfy the predicate:
Once the input is written as a word, deciding the problem is the same as answering a membership problem:
given a word , is ? Conversely, every language poses such a question, with the predicate = "".
For the opening question, is the set of legal names: total_2 belongs to it and 2total does not.
What do the names Python accepted have in common? Describing that is describing .
Defining an infinite set
From the discrete mathematics course we know the inductive definition of an infinite set (starting elements and rules that build new elements from old ones) and the definition of a function by recursion along such a definition, and this is how we define words and the operations on them.
Alphabet and words
An alphabet, written , is a finite, nonempty set of letters, for example or .
Real alphabets include the bits of a file, the four DNA letters , and the characters of Unicode.
When we speak about an arbitrary letter, as with a variable, we write a lowercase Latin letter such as .
The empty word is written (epsilon). It is like "" in programming:
a real word with no letters, not “nothing”.
Let denote the set of all words over , that is, the set of all strings that can be formed from the letters of .
Look at the words over by their number of letters:
| Number of letters | Words |
|---|---|
| 0 | |
| 1 | |
| 2 |
Formulate it yourselves. How do we build all the words, starting from and adding one letter at a time?
Write an inductive definition in your own words: what do we start from, and which rule creates new words from words already built?
Only after you have written a definition of your own, open the candidate definitions and choose the one you think is right:
Candidate definitions
In every option are words and is a letter.
A: base ; rule: if then
The thinking: start from the letters themselves and append one letter at each step.
The mistake: the empty word is not in the base and cannot be reached from any word, so .
But is a word (like "" in programming). This definition gives the set of nonempty words.
B: base ; rule: if then
The thinking: start from the empty word, and at each step append one letter on the right of a word already built.
This is the definition we wanted. Check on : gives , and gives .
Appending on the left gives the same set; by convention we append on the right.
C: base ; rule: if then
The thinking: from we reach the letters.
The mistake: the rule does not use , only the letter, so it creates only and the single letters.
The word is never created.
D: base ; rule: if then
The thinking: glue two words that were already built.
The mistake: the rule glues existing words and the base holds only .
Gluing to gives again, so the set is just .
The rule never introduces letters.
Hint 1
Build the table by hand: from we reach and .
From which words, and in which step, do we reach and ?
Hint 2
An inductive definition needs two parts: a base word that exists without being built, and a rule that takes a word already built and a letter and computes a new word from them.
The official definition
- base: ;
- rule: if and then (append one letter on the right).
In the formula sheets the definition is written with the inductive-set notation , the set generated from a base set by generating functions , as in the discrete mathematics course.
The same notation also gives , the set of nonempty words:
The base of is the empty word, the base of is the single letters, and the one generating function appends a letter on the right.
Every nonempty word can be written uniquely as with a word and its last letter.
Functions on words are defined by recursion on this last letter.
The set of nonempty words is written and defined by .
Check yourself. Answer each part before you open its answer.
(a) Is ?
Answer
Yes: it is the base case of the definition.
(b) List all the words in that have at most two letters.
Answer
: seven words. From we append a letter on the right, and then one more letter on the right.
(c) Is ? Build it from the definition.
Answer
Yes. When : by the base case, then by the rule, and then by the rule.
The length of a word
Length: let us build it together. The length of a word is the number of its letters, written (what len(s) returns for a string s in Python).
Some examples first:
| word | ||||
|---|---|---|---|---|
| length | 0 | 1 | 2 | 3 |
Question 1. Every nonempty word has the form (a word followed by one last letter ).
Suppose you already know the length of . What is the length of ?
And what should the length of be? Write down a definition in your own words before you open anything below.
Only after you have written a definition of your own, open the candidate definitions and choose the one you think is right:
Candidate definitions
In every option is a word and is a letter.
A:
The thinking: the length of the word before the last letter, with the base value 0.
The mistakes: appending a letter never changes the length in this rule, so every word would have length 0.
Check it on : it should be 1.
B:
The thinking: the step rule is right, only the base value is different.
The mistakes: the empty word has no letters, so its length is 0.
With the base value 1, every word would be one too long: this rule gives .
C:
The thinking: the length of is the length of plus the length of the letter .
The mistakes: this is true, but it quietly uses , which is exactly the one-letter case we are defining.
It does not tell us why a letter has length 1, so it is not yet a definition.
D:
The thinking: no letters in , and each appended letter adds one.
This is the definition we want. Check it on : .
Hint 1
Compare with , and with . What changed in the word, and what changed in the length?
Hint 2
A recursive definition needs two parts: a value for the base word , and a rule that gets the value for from the value for .
The official definition, and how to run it on a word
Written as cases, as in a formula sheet (here and below, is a word and a letter):
For example:
In each step the rule for the last letter replaces by , then by , and finally comes from the base case.
What if? Suppose we defined the length by recursion on the first letter instead, , with the same two rules.
Would any word get a different length?
Answer
No. Both definitions count one for every letter, so they agree on every word.
Recursion on the last letter is only our convention, because words grow on the right in the inductive definition.
Counting a letter
The number of occurrences of the letter in the word is written .
For example and .
Formulate. Define by recursion on the last letter, as we defined the length:
what is ? And if (with ) and we already know , what is ?
Hint
What changes in the count when the last letter is , and what when it is another letter?
The official definition
Here is a word and is any letter other than :
Question 2. Use the definition of , answering each part before you open its answer.
(a) Which line of the definition applies to at the first step?
Answer
The middle line, because the last letter is : .
(b) How much is ?
Answer
Continue: , then (the last letter is , so the last line applies).
Total: 2.
Concatenation of words
Question 3. Concatenation places one word after another, and we usually write .
The notation means the same and is used when the operation must be visible.
Define by recursion on the second word: what is , and how do you get from ?
Hint
Try . Append the letters of the second word to one at a time.
The official definition
Order matters: but .
Why the length of a concatenation is the sum of the lengths (proof by induction)
For example . We prove that this holds for all words.
Claim. For all words : .
Proof, by induction on (with an arbitrary fixed word).
Let be the statement .
Base case, . Then , so .
Induction hypothesis. Assume holds for some word .
Induction step. We prove for every letter :
The first equality is the definition of concatenation, the second and the last are the definition of length, and the third is the induction hypothesis.
Conclusion. Every word is or is built from by appending letters, so holds for every word .
Legal names as a language
After concatenation and length we can return to the question we opened with:
what the names that Python accepted have in common. Suppose is the alphabet of the Latin letters, the digits and the underscore _, and we ignore reserved words such as class.
Formulate. Write a rule on a word that says when it is a legal name.
What must hold for its first letter, and what for its length?
Hint
Compare total_2 with 2total: how do they differ? And what about the empty word?
Answer
That is, the name is not empty and its first letter is not a digit.total_2 belongs to the language (the first letter is ), 2total does not (its first letter is a digit), and the empty word does not (it has no first letter).
Powers of a word
Here , where includes zero and means the positive numbers.
Question 4. We want . The power is repeated times.
Define by recursion on : what is , and how do you get from ?
Hint
Compare with . What is if repeating zero times should change nothing under concatenation?
The official definition
Question 5. Answer each part before you open its answer.
(a) What is ?
Answer
: zero repetitions give the empty word.
(b) What is ?
Answer
.
(c) What is for a word ?
Answer
Each repetition adds letters, so .
The reverse of a word
Question 6. The reverse lists the letters backwards, so and .
Suppose with and , and you already know .
How do you build from it? What is ?
Hint
Where does the last letter of end up in :
at the beginning or at the end? Test on , with .
The official definition
so:
Question 7. What is in terms of and ?
Hint
Try and . What is , and what is ?
Answer
: the order is reversed. and , whereas is different.
Proof by induction of the rule for the reverse of a concatenation
Claim. For all words : .
Proof, by induction on (with an arbitrary fixed word).
Let be the statement .
Base case, . Then , so .
Induction hypothesis. Assume holds for some word .
Induction step. We prove for every letter :
The first equality is the definition of concatenation, the second and the last are the definition of reverse ( for ), and the third is the induction hypothesis.
Conclusion. Every word is or is built from by appending letters, so holds for every word .
Counting words
For an alphabet with two letters, such as , there are words of length :
two choices for each of the positions. So is infinite although every word in it is finite.
Question 8. Answer each part before you open its answer.
(a) How many words of length are there over ?
Answer
There are of them: two choices for each of the three positions.
(b) How many words of length at most are there?
Answer
Adding the lengths gives words.
See Languages: operations and equalities, where we collect words into languages.