Lecture 20: formal languages, inductive definitions, rule induction [016F]
- February 7, 2025
- Jon Sterling, with contributions from Andrew Pitts, Ian Leslie, Frank Stajano
These lecture notes were prepared by Jon Sterling by adapting prior notes created by Andrew Pitts that I inherited from Frank Stajano.
Formal languages [016G]
- February 7, 2025
- Jon Sterling
Definition Alphabet [016H]
- February 7, 2025
- Jon Sterling
An alphabet is specified by giving a finite set \Sigma whose elements are called symbols. Any finite set can serve as an alphabet as far as we are concerned.
Definition String over an alphabet [016I]
- February 7, 2025
- Jon Sterling
Let \Sigma be an alphabet. A string of length n over \Sigma is defined to be an n-tuple of elements of \Sigma . We write these without any punctuation between them. For example, aaabc is a string over the Latin alphabet; we will write \epsilon for the empty string. The set of all strings (of any length) over \Sigma is written \Sigma ^\star ; for a string u:\Sigma ^\star , we will write {\mathopen {}\left \lvert u\right \rvert \mathclose {}}:\mathbb {N} for its length.
Definition Concatenation of strings [016J]
- February 7, 2025
- Jon Sterling
Lemma Properties of concatenation [016K]
- February 7, 2025
- Jon Sterling
Concatenation satisfies several algebraic laws:
- The empty string \epsilon is a neutral element for concatenation on both the left and right right: \forall u:\Sigma ^\star \mathpunct {.} \epsilon u = u\land u\epsilon = u
- Concatenation is associative: \forall u,v,w:\Sigma ^\star \mathpunct {.} u(vw) = (uv)w
Observation Monoid structure of strings [016L]
- February 7, 2025
- Jon Sterling
By Lemma [016K], strings under concatenation form a monoid, but not a commutative monoid (indeed, we don’t have uv=vu unless either u or v is empty or u=v). Viewing \Sigma ^\star as a monoid, we see that the length function {\mathopen {}\left \lvert -\right \rvert \mathclose {}}\colon \Sigma ^\star \to \mathbb {N} is a homomorphism of monoids from strings to the additive monoid of the natural numbers:
\begin {aligned} {\mathopen {}\left \lvert \epsilon \right \rvert \mathclose {}} &= 0\\ {\mathopen {}\left \lvert uv\right \rvert \mathclose {}} &= {\mathopen {}\left \lvert u\right \rvert \mathclose {}}+{\mathopen {}\left \lvert v\right \rvert \mathclose {}} \end {aligned}We are now ready to define what a formal language is. We are used to thinking of languages as being defined by a grammar, but a more abstract or “extensional” point of view is that a formal language is nothing more than the set of strings it generates. We adopt that perspective below.
Definition Formal language [016M]
- February 7, 2025
- Jon Sterling
Although we have defined formal languages in this very extensional manner, “rules of grammar” still plays a role: in particular, we will use inductive definitions of grammatical rules to generate formal languages by building on our prior experience with inductive definitions:
- In Lecture 14, we learned how to define the reflexive-transitive closure of a binary relation on a set, i.e. the path relation on a directed graph.
- In Lecture 15, we learned how to define a function inductively by taking the least relation “closed” under some clauses.
In what remains of this lecture, we will be studying strategies for defining and using inductive definitions of subsets of a given set.
Inductive definitions [016N]
- February 7, 2025
- Jon Sterling
Axioms and rules [016O]
- February 7, 2025
- Jon Sterling
In many cases, a subset is defined implicitly by describing the rules that you must follow in order to construct an element of it.
Example Informal rules for constructing naturals [016Q]
- February 7, 2025
- Jon Sterling
For example, consider the set of natural numbers \mathbb {N} viewed as a subset of the reals \mathbb {R}; this could be described by the following rules of construction:
- 0 is a natural number.
- If x is a natural number, then x+1 is also a natural number.
The set of natural numbers is then described as the smallest subset of \mathbb {R} that obeys these rules, i.e. the intersection of all the subsets of \mathbb {R} that contain 0 and are closed under the successor function x \mapsto x+1.
This is the same process as we saw when constructing inductively defined functions, where we first defined the equational rules we wanted the function to follow as a closure condition on a relation and then we defined the function as the smallest relation satisfying the closure condition.
Example Inductive definition of the naturals via closure conditions [016R]
- February 7, 2025
- Jon Sterling
In Example [016Q] we described the rules for constructing natural numbers in plain English. Formally, the two rules above would be understood each as a subset of \mathcal {P}{\mathopen {}\left (\mathbb {R}\right )\mathclose {}}.
\begin {aligned} \mathsf {ZeroClosed},\mathsf {SuccClosed} &\subseteq \mathcal {P}{\mathopen {}\left (\mathbb {R}\right )\mathclose {}} \\ S\in \mathsf {ZeroClosed} &\Leftrightarrow 0\in S\\ S\in \mathsf {SuccClosed} &\Leftrightarrow \forall x\in S\mathpunct {.} x+1\in S \end {aligned}Then natural numbers would be defined to be the smallest subset of \mathbb {R} that lies both in \mathsf {ZeroClosed} and \mathsf {SuccClosed}:
\mathbb {N} = \bigcap {\mathopen {}\left \{S\subseteq \mathbb {R}\,\middle \vert \,S\in \mathsf {ZeroClosed}\cap \mathsf {SuccClosed}\right \}\mathclose {}}Warning Closure conditions must be closed under intersection [0172]
- February 8, 2025
- Jon Sterling
In general, for the definition of \mathbb {N}\subseteq \mathbb {R} in Example [016R] to work, the subsets of \pow {X} must themselves be closed under intersection: otherwise, taking the big intersection to get the smallest subset “closed under the rules” might not work because the intersection might no longer be closed under the rules. So, for example, you need to check that if you have a family of subsets X_i\subseteq \mathbb {R}, then if each X_i\in \mathsf {SuccClosed} we also have \bigcap _i X_i\in \mathsf {SuccClosed}. But this is nearly always obvious for closure conditions encountered in practice.
Syntactic description of rules [016P]
- February 7, 2025
- Jon Sterling
The plain-English description of rules of construction that we saw in Example [016Q] are a bit lengthy to read and write; by contrast, the more precise depiction of rules as subsets of the powerset that we saw in Example [016R] is perhaps overly formal and obfuscated. For this reason, computer scientists and logicians have developed an alternative notation for describing rules of construction that involves drawing things above and below horizontal lines (these are not fractions!).
Definition Syntactic rule [016S]
- February 7, 2025
- Jon Sterling
The basic form of a rule that describes a subset of a set X is as follows:
\frac { u_1 \quad \ldots \quad u_n }{ v }The plain English meaning of the rule is: If u_1 through u_n are elements of the subset that I am defining, then so is v. In such a rule, the items on top of the horizontal line are called premises or antecedents or hypotheses, and the item on the bottom of the horizontal line is called the conclusion. When a rule has no premises, it is called an axiom.
Example Syntactic rules for forming natural numbers [016T]
- February 7, 2025
- Jon Sterling
We can translate the plain-English description of the natural numbers into the syntactic form specified by Definition [016S]. In particular, the rules expression closure under zero and successor are rendered as follows:
\displaystyle \frac {\displaystyle }{\displaystyle \quad 0\quad } \qquad \displaystyle \frac {\displaystyle x}{\displaystyle \quad x+1\quad }The above are to be understood as syntactic rules describing a subset of \mathbb {R}. The first rule is an axiom in the sense of our definition, because it has no premises.
Definition The closure condition denoted by a syntactic rule [016U]
- February 7, 2025
- Jon Sterling
A syntactic rule like \scriptstyle \frac {\scriptstyle u_1\quad \ldots \quad u_n}{\scriptstyle v} on a set X denotes a closure condition on subsets of X, i.e. a subset of the powerset \mathcal {P}{\mathopen {}\left (X\right )\mathclose {}}. In particular, the meaning of this rule is the following subset of \mathcal {P}{\mathopen {}\left (X\right )\mathclose {}}:
{\mathopen {}\left \{S\subseteq X\,\middle \vert \, u_1,\ldots ,u_n\in S\implies v\in S \right \}\mathclose {}}When \mathcal {R} is a set of rules over X, we will write \mathrm {Cl}_{\mathcal {R}}\subseteq \mathcal {P}{\mathopen {}\left (X\right )\mathclose {}} for the intersection of the closure conditions denoted by each rule in \mathcal {R}. This is, in other words, the set of subsets of X that lie in the closure condition for each rule r\in \mathcal {R}.
In light of Definition [016U], we see that the closure conditions denoted by the syntactic rules for the naturals in Example [016T] are precisely those specified in Example [016R].
Definition Derivations [016X]
- February 7, 2025
- Jon Sterling
In the presence of a set \mathcal {R} of syntactic rules describing a subset of X, a derivation that a particular element u lies in that subset is given by a finite tree with vertices labelled by elements of X such that:
- the root of the tree is u;
- each vertex of the tree is the conclusion of a rule whose premises are the children of the node
- each leaf of the tree is an axiom.
Viewing a tree as growing upwards from the root, we have the following derivation for 3\in \mathbb {N} in when \mathcal {R} is the two rules from Example [016T]:
\displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad 0\quad } }{\displaystyle \quad 1\quad } }{\displaystyle \quad 2\quad } }{\displaystyle \quad 3\quad }We recall the definition of a formal language:
Definition Formal language [016M]
- February 7, 2025
- Jon Sterling
Because a formal language is nothing more than a subset, we can define them by rules of construction—either in the form of plain English rules, closure conditions, or syntactic rules. We will focus on the use of syntactic rules and their relationship to the closure conditions they denote.
Definition Syntactic presentation of a formal language [016W]
- February 7, 2025
- Jon Sterling
A syntactic presentation of a formal language over an alphabet \Sigma is defined to be a set of syntactic rules over the set of strings \Sigma ^\star .
Example A syntactic presentation of a formal language [016Y]
- February 8, 2025
- Jon Sterling
We can present an example formal language over the alphabet \Sigma = {\mathopen {}\left \{a,b\right \}\mathclose {}} by a set of rules:
\displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } \quad \displaystyle \frac {\displaystyle u}{\displaystyle \quad aub\quad } \quad \displaystyle \frac {\displaystyle u}{\displaystyle \quad bua\quad } \quad \displaystyle \frac {\displaystyle u\quad v}{\displaystyle \quad uv\quad }The string abaabb can be generated from these rules in at least two ways by means of the following derivations:
\displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } }{\displaystyle \quad ab\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } }{\displaystyle \quad ab\quad } }{\displaystyle \quad aabb\quad } }{\displaystyle \quad abaabb\quad } \qquad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } }{\displaystyle \quad ba\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } }{\displaystyle \quad ab\quad } }{\displaystyle \quad baab\quad } }{\displaystyle \quad abaabb\quad }On the other hand, there is no derivation for the string abaab.
Example Revisiting reflexive-transitive closure [016Z]
- February 8, 2025
- Jon Sterling
Let R\colon X\mathbin {\nrightarrow } X be a binary relation on a set X. In Lecture 14 we saw that the reflexive-transitive closure of R can be defined in two equivalent ways:
- Concretely: it can be defined to be the smallest relation closed under all finite paths in R, viewed as a directed graph.
- Abstractly: it is the smallest preorder containing R as a subrelation.
There is a “middle ground” between the most concrete and abstract ways of looking at reflexive-transitive closure: we can present it by means of syntactic rules whose induced closure conditions isolate the “set of preorders containing R as a subrelation”.
We first add the following rules over X\times X that correspond to the laws of a preorder.
\displaystyle \frac {\displaystyle }{\displaystyle \quad (x,x)\quad } \quad \displaystyle \frac {\displaystyle (x,y)\quad (y,z)}{\displaystyle \quad (x,z)\quad }Finally, we add the following rule for any x,y\in X such that x\mathrel {R}y:
\displaystyle \frac {\displaystyle }{\displaystyle \quad (x,y)\quad } \quad (x\mathrel {R}y)To be very clear, this last rule only applies when x\mathrel {R}y. This is called a side condition.
In order to see that the rules described in Example [016Z] actually define the reflexive transitive closure in the sense that a pair (x,y) has a derivation if and only if there is a path from x to y, we will need the rule induction theorem, which is the topic of the rest of this lecture.
The rule induction principle [0170]
- February 8, 2025
- Jon Sterling
We have been dancing around the relationship between sets of syntactic rules over a set X the subsets of X that they carve out by means of derivations. Rule induction is the principle that allows us to prove that these two perspectives are equivalent.
Theorem Rule induction [0171]
- February 8, 2025
- Jon Sterling
Let \mathcal {R} be a set of syntactic rules over a set X. Let X_{\mathcal {R}}\subseteq X be the subset containing just those x\in X such that there exists a derivation of x in \mathcal {R}.
Then X_{\mathcal {R}} is closed under each of the rules in \mathcal {R} and it is moreover the smallest such subset of X. In other words, X_{\mathcal {R}}\in \mathrm {Cl}_{\mathcal {R}} and if S\in \mathrm {Cl}_{\mathcal {R}} then X_{\mathcal {R}}\subseteq S. Equivalently, X_{\mathcal {R}} = \bigcap \mathrm {Cl}_{\mathcal {R}}.
Proof.
- February 8, 2025
- Jon Sterling
We first check that X_{\mathcal {R}} lies in \mathrm {Cl}_{\mathcal {R}}. We first fix a rule in \mathcal {R}:
\displaystyle \frac {\displaystyle u_1\quad \ldots \quad u_n}{\displaystyle \quad v\quad }We must show that X_{\mathcal {R}} satisfies the closure condition described by the rule depicted above. For that, we assume that u_1,\ldots ,u_n lie in X_{\mathcal {R}} and we need to verify that v lies in X_{\mathcal {R}}. By definition, we therefore have \scriptstyle \frac {\scriptstyle \vdots }{\scriptstyle u_i} derivations of each u_i and we need to find a derivation of v. We do this by combining the assumed derivations using the rule as follows to obtain a derivation of v:
\displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \vdots }{\displaystyle \quad u_1\quad } \quad \ldots \quad \displaystyle \frac {\displaystyle \vdots }{\displaystyle \quad u_n\quad } }{\displaystyle \quad v\quad }We have therefore shown that X_{\mathcal {R}}\in \mathrm {Cl}_{\mathcal {R}}.
For the remainder of the proof, we fix any S\in \mathcal {Cl}_{\mathcal {R}} to check that X_{\mathcal {R}}\subseteq S. In other words, we must prove:
For all x: X such that x has a derivation in \mathcal {R}, we have x\in S.
But the above can be rephrased as follows:
For all derivations d generated by \mathcal {R}, the conclusion of d lies in S.
As every derivation has a (maximum) height, we can further rephrase the goal in terms that will be amenable to the use of mathematical induction.
For all n\in \mathbb {N}, the conclusion of any derivation with height less than or equal to n lies in S.
We now proceed by induction.
- In the base case, we must show that the conclusion of any derivation d with height less than or equal to 0 lies in S. But any derivation has height at least 1, so this is vacuous.
In the inductive step, we suppose that the conclusion of every derivation of height less than or equal to n lies in S; we must prove that the conclusion of every derivation of height less than or equal to n+1 lies in S.
To that end, consider a derivation of height less than or equal to n+1 with conclusion v, which we might depict below as follows:
\displaystyle \frac {\displaystyle \frac {\vdots }{u_1}\quad \ldots \quad \frac {\vdots }{u_n} }{\displaystyle \quad v\quad }Such a derivation must come from a rule of the following form in \mathcal {R}:
\displaystyle \frac {\displaystyle u_1\quad \ldots \quad u_n}{\displaystyle \quad v\quad }We need to show that v lies in S. Naturally, all the subderivations of u_i depicted above have height less than or equal to n; therefore, our inductive hypothesis ensures that each u_i lies in S.
The rule \scriptstyle \frac {\scriptstyle u_1\quad \ldots \quad u_n}{\scriptstyle v} specifies a closure condition, and because S\in \mathrm {Cl}_{\mathcal {R}} we know that S is closed under it. Recalling the closure condition induced by a rule, we can immediately conclude v\in S from u_1,\ldots ,u_n\in S.
By virtue of Theorem [0171], we are justified in referring to X_{\mathcal {R}} as the subset of X inductively defined by \mathcal {R}.
Example Application of rule induction [0173]
- February 8, 2025
- Jon Sterling
We recall the formal language over \Sigma ={\mathopen {}\left \{a,b\right \}\mathclose {}} presented in Example [016Y] by the following rule set \mathcal {R}:
\displaystyle \frac {\displaystyle }{\displaystyle \quad \epsilon \quad } \quad \displaystyle \frac {\displaystyle u}{\displaystyle \quad aub\quad } \quad \displaystyle \frac {\displaystyle u}{\displaystyle \quad bua\quad } \quad \displaystyle \frac {\displaystyle u\quad v}{\displaystyle \quad uv\quad }For any string u\in {\mathopen {}\left \{a,b\right \}\mathclose {}}^\star , let P(u) be the property that u contains the same number of a and b symbols. Then for all u\in \Sigma ^\star _{\mathcal {R}}, we have P(u).
Proof.
- February 8, 2025
- Jon Sterling
We proceed by rule induction, letting S = {\mathopen {}\left \{u\,\middle \vert \,P(u)\right \}\mathclose {}}. In particular, we need only show that S is closed under each of the rules:
- We note that the empty string has the same (zero) number of a and b symbols.
- Adding both an a and a b to a given string in whatever location preserves the relationship between the number of a and b symbols.
- If u has n copies of a and b each and v has m copies of a and b each, then the concatenation uv has m+n copies of a and b each.
Therefore S\in \mathrm {Cl}_{\mathcal {R}} and thus \Sigma ^\star _{\mathcal {R}}\subseteq S.