Lecture 23: regular languages and Kleene’s theorem [017X]
- February 14, 2025
- Jon Sterling, with contributions from Andrew Pitts, Ian Leslie, Frank Stajano
- IA Discrete Mathematics
These lecture notes were prepared by Jon Sterling by adapting prior notes created by Andrew Pitts that I inherited from Frank Stajano.
Regular vs. automatical languages [018C]
- February 16, 2025
- Jon Sterling
In previous lectures, we have seen two apparently different ways to generate a class of formal languages: by means of regular expressions and by means of finite automata. The goal of this lecture is to see that all these classes of language coincide, which is a theorem due to Stephen Kleene; to start with, we define regular languages below.
Definition Regular language [017Y]
- February 14, 2025
- Jon Sterling
A formal language is called regular if it is equal to \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} for some regular expression R.
Using a bit of non-standard terminology, we give a name to the class of languages that can be recognised by finite automata.
Definition Automatical language [0185]
- February 14, 2025
- Jon Sterling
A formal language is called automatical if it is equal to \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} for some finite automaton M.
By virtue of Corollary [017U], it does not matter at all whether Definition [0185] is understood to be referring to DFAs, NFA, or NFA\varepsilon s: the conditions are all equivalent.
When we wanted to show that different kinds of automata generated the same class of languages, we first described a method to convert between them: in particular, the subset construction converts an NFA\varepsilon to a DFA that accepts the same language, as we proved in Corollary [017U]. Today, we will use a similar strategy: we will show in § [0184] how to compile a regular expression to an NFA accepting the same language, and vice versa in [017Z]; from this we will conclude Kleene’s theorem, which states that a language is regular if and only if it is automatical.
Compiling a regular expression to an NFA [0184]
- February 14, 2025
- Jon Sterling
We aim to show that every regular language is automatical. The rule induction principle for regular expressions implies that the regular languages are the smallest class of formal languages closed under unions, concatenation, repetition, single symbols, the empty string, and the empty language. Therefore, to prove our desired theorem it will suffice to show that the automatical languages are closed under the same constructions.
Lemma Automaticality of basic regular languages [0186]
- February 14, 2025
- Jon Sterling
Let \Sigma be an alphabet. Then the basic regular languages {\mathopen {}\left \{a\right \}\mathclose {}} = \mathcal {L}{\mathopen {}\left (a\right )\mathclose {}} for a:\Sigma , and {\mathopen {}\left \{\epsilon \right \}\mathclose {}} = \mathcal {L}{\mathopen {}\left (\underline {\epsilon }\right )\mathclose {}}, and \varnothing = \mathcal {L}{\mathopen {}\left (\varnothing \right )\mathclose {}} are all automatical.
Proof.
- February 14, 2025
- Jon Sterling
Lemma Closure of automaticality under union [0187]
- February 14, 2025
- Jon Sterling
If L_1 and L_2 are automatical languages over a common alphabet \Sigma , then so is their union L_1\cup L_2.
Proof.
- February 14, 2025
- Jon Sterling
Let M_1 and M_2 be finite automata with \mathcal {L}{\mathopen {}\left (M_i\right )\mathclose {}}=L_i; without loss of generality, we assume these have disjoint sets of states. We shall describe an NFA\varepsilon M with \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M_1\right )\mathclose {}}\cup \mathcal {L}{\mathopen {}\left (M_2\right )\mathclose {}}.
We define an accepting state of M above to be either an accepting state of M_1 or of M_2. F_{M} = F_{M_1}\cup F_{M_2}
That \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M_1\right )\mathclose {}}\cup \mathcal {L}{\mathopen {}\left (M_2\right )\mathclose {}} holds follows by a quick inspection: indeed, if u\in \mathcal {L}{\mathopen {}\left (M_i\right )\mathclose {}} then we have a path in M_i from \star {M_i} to an accepting state in M_i, which we can extend by means of either \varepsilon -transition to a path in M. Conversely, any from q_0 to an accepting state of M must either be a path to an accepting state of some M_i and must therefore pass through one of the two \varepsilon -transitions. Chop off that transition to get a valid path in either M_1 or M_2.
That \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M_1\right )\mathclose {}}\mathcal {L}{\mathopen {}\left (M_2\right )\mathclose {}} holds by inspection.
Lemma Closure of automaticality under concatenation [0188]
- February 14, 2025
- Jon Sterling
If L_1 and L_2 are automatical languages over a common alphabet \Sigma , then so is their concatenation L_1L_2, which we defined below:
L_1L_2 = {\mathopen {}\left \{uv :\Sigma ^\star \,\middle \vert \,u\in L_1, v\in L_2\right \}\mathclose {}}Proof.
- February 14, 2025
- Jon Sterling
Let M_1 and M_2 be finite automata with \mathcal {L}{\mathopen {}\left (M_i\right )\mathclose {}}=L_i; without loss of generality, we assume that these have disjoint sets of states. We shall describe an NFA\varepsilon M with \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M_1\right )\mathclose {}}\mathcal {L}{\mathopen {}\left (M_2\right )\mathclose {}}; in particular, we lay out M_1 and M_2 side by side and draw an \varepsilon -transition from every accepting node of M_1 to the staring node of M_2.
The starting node of M is that of M_1 and the accepting nodes of M are those of M_2.
That \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M_1\right )\mathclose {}}\mathcal {L}{\mathopen {}\left (M_2\right )\mathclose {}} holds by inspection.
Lemma Closure of automaticality under repetition [0189]
- February 14, 2025
- Jon Sterling
If L is an automatical language over a given alphabet \Sigma , then so is the repetition L^\star of L, which we define below in terms of concatenation:
L^\star = {\mathopen {}\left \{\epsilon \right \}\mathclose {}} \cup L \cup LL \cup \ldots = \bigcup _{n:\mathbb {N}} {\mathopen {}\left \{u_1\ldots u_n\,\middle \vert \,u_i\in L\right \}\mathclose {}}Proof.
- February 14, 2025
- Jon Sterling
Let M be a finite automaton with \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=L. We shall describe an NFA\varepsilon M^\star satisfying \mathcal {L}{\mathopen {}\left (M^\star \right )\mathclose {}}=L^\star ; we take M and add a new starting state q_0 with an \varepsilon -transition to the old starting state, and then we make an \epsilon -transition from every old accepting state back to q_0.
The only accepting state of M^\star is q_0.
It holds by inspection that \mathcal {L}{\mathopen {}\left (M^\star \right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}^\star .
Corollary All regular languages are automatical [018A]
- February 16, 2025
- Jon Sterling
If a language L is regular, then it is automatical.
Proof.
- February 16, 2025
- Jon Sterling
Let U be the set of all regular expressions R over a fixed alphabet \Sigma such that \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} is automatical. We wish to show that the set of all regular expressions over \Sigma is contained in U. By rule induction it suffices to check the following cases:
- We need to know that \texttt {Sym}_{a},\texttt {Null}, \texttt {Never} \in U, which is the same as to say that the languages {\mathopen {}\left \{a\right \}\mathclose {}}, {\mathopen {}\left \{\epsilon \right \}\mathclose {}}, \varnothing are all automatical. This is Lemma [0186].
- We need to show that if R,S\in U then \texttt {Union}{\mathopen {}\left (R,S\right )\mathclose {}}\in U, which is to say that the language \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\cup \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} is automatical assuming that \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} and \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} are. This is Lemma [0187].
- We need to show that if R,S\in U then \texttt {Concat}{\mathopen {}\left (R,S\right )\mathclose {}}\in U, which is to say that the language \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} = {\mathopen {}\left \{uv\,\middle \vert \,u\in \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}},v\in \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\right \}\mathclose {}} is automatical assuming that \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} and \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} are. This is Lemma [0188].
- We need to show that if R\in U then so is \texttt {Star}{\mathopen {}\left (R\right )\mathclose {}}\in U, which is to say that if \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} is automatical then so is \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}^\star . This is Lemma [0189].
We are done.
Decompiling a regular expression from an NFA [017Z]
- February 14, 2025
- February 14, 2025
- Jon Sterling
We want to “decompile” any NFA to an equivalent regular expression; it is not easy to do so directly, so we will actually solve a more general problem first.
Definition Region of an NFA [0181]
- February 14, 2025
- Jon Sterling
Let M be an NFA. A region of M is defined to be a triple {\mathopen {}\left (q,S,q'\right )\mathclose {}} with S\subseteq Q_{M} and q,q'\in Q_{M}.
A region {\mathopen {}\left (q,S,q'\right )\mathclose {}} in an NFA M is meant to encode a language whose strings consist of those that can be consumed starting from states q and ending in state q' passing only through states that lie in S. We define this more formally below.
Definition The formal language accepted within a region [0182]
- February 14, 2025
- Jon Sterling
Let {\mathopen {}\left (q,S,q'\right )\mathclose {}} be a region in an NFA M. The formal language accepted within {\mathopen {}\left (q,S,q'\right )\mathclose {}} is defined to be the following set of strings:
\mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} = {\mathopen {}\left \{u :\Sigma _M^\star \,\middle \vert \, q \mathrel {\vphantom {\xrightarrow {u}}\smash {\xrightarrow {u}}^*} q' \text { with all intermediate states lying in S} \right \}\mathclose {}}We shall also refer to \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} as the regional language of M in {\mathopen {}\left (q,S,q'\right )\mathclose {}}.
Lemma Generalised NFA decompilation [0180]
- February 14, 2025
- Jon Sterling
Any regional language of an NFA is regular.
Proof.
- February 14, 2025
- Jon Sterling
We proceed by induction on the number of elements in S.
In the base case, we know that S is empty; the regional language \mathcal {L}_{M}{\mathopen {}\left (q,\varnothing ,q'\right )\mathclose {}} therefore consist of those strings u such that either u=\epsilon and q=q', or u=a for some a:\Sigma _M and q\mathrel {\xrightarrow {a}}q'. Writing this more formally, we have:
\mathcal {L}_{M}{\mathopen {}\left (q,\varnothing ,q'\right )\mathclose {}} = {\mathopen {}\left \{\epsilon \,\middle \vert \,q=q'\right \}\mathclose {}}\cup \bigcup {\mathopen {}\left \{{\mathopen {}\left \{a\right \}\mathclose {}}\,\middle \vert \,q\mathrel {\xrightarrow {a}}q'\right \}\mathclose {}}The above is a finite union of regular languages, and so it is evidently regular.
In the inductive step, we assume that any regional language whose interior state set has n elements is regular, and we must show the same for any regional language whose interior state set has n+1 elements. Fixing such a region {\mathopen {}\left (q,S,q'\right )\mathclose {}} with \#S=n+1, we will try to reduce the \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} to something in terms languages that we know by our inductive hypothesis to be regular. Our strategy is to simply remove an arbitrary element from S.
To that end, we pick some arbitrary q_0\in S so that the set difference S' = S\setminus {\mathopen {}\left \{q_0\right \}\mathclose {}} has n elements. IF we think about all the ways taht we could get from q to q' passing through states lying only in S, there are a two possibilities in relation to the state q_0:
- We might have never pass through q_0 at all. This would correspond to the regional language \mathcal {L}_{M}{\mathopen {}\left (q,S',q'\right )\mathclose {}}, which we know to be regular by our induction hypothesis.
- If we do pass through q_0, we must first go from q to q_0 without passing through q_0, then we return to q_0 arbitrarily many times, and then we finally go from q_0 to q without passing through q_0. This corresponds to the following concatenation of languages, which we know to be regular by our induction hypothesis and the fact that regular languages are closed under concatenation and repetition: \mathcal {L}_{M}{\mathopen {}\left (q,S',q_0\right )\mathclose {}} \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q_0\right )\mathclose {}}^\star \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q'\right )\mathclose {}}
We see the following by inspection:
\mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} = \mathcal {L}_{M}{\mathopen {}\left (q,S',q\right )\mathclose {}} \cup {\mathopen {}\left ( \mathcal {L}_{M}{\mathopen {}\left (q,S',q_0\right )\mathclose {}} \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q_0\right )\mathclose {}}^\star \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q'\right )\mathclose {}} \right )\mathclose {}}As regular languages are closed under finite unions, we are done.
Corollary Decompilation of NFAs [0183]
- February 14, 2025
- Jon Sterling
Any automatical language is regular.
Proof.
- February 14, 2025
- Jon Sterling
We have shown in Lemma [0180] that each regional langauge of an NFA is regular; regular languages are closed under finite unions, so it suffices to decompose \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} into a finite union of regional languages \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}}.
We note that if a string is accepted by M, it does so by means of a path from the starting state to some accepting state; each of these scenarios for a given accepting state specifies a region {\mathopen {}\left (s_{M},Q_{M},q\right )\mathclose {}} with q\in F_{M}. Therefore, we immediately deduce:
\mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} = \bigcup _{q\in F_{M}} \mathcal {L}_{M}{\mathopen {}\left (s_{M},Q_{M},q\right )\mathclose {}}Therefore it follows that \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} is regular.
Kleene’s theorem and its consequences [018D]
- February 16, 2025
- Jon Sterling
We now come to the main result of this lecture.
Theorem Kleene’s theorem [018B]
- February 16, 2025
- Jon Sterling
A formal language is regular if and only if it is automatical.
Proof.
- February 16, 2025
- Jon Sterling
In light of the above, we will from now on refer mainly to regular languages, and leave behind the non-standard terminology of “automatical languages”. Several questions that we left unanswered (see Discussion [017F]) can now be addressed in light of Kleene’s theorem.
Corollary Decidability of regular expression matching [018E]
- February 16, 2025
- Jon Sterling
Let R be a regular expression over an alphabet \Sigma . Then there exists an algorithm to compute whether a given string u:\Sigma ^\star matches R.
Proof.
- February 16, 2025
- Jon Sterling
The idea of the proof is to find a DFA M satisfying \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}; so for this we need only Corollary [018A] rather than the full Kleene’s theorem. Then our algorithm is to feed the given string u through M and check whether the final state is accepting or not.
There is a subtlety in the argument above: we need the DFA M to have computably decidable equality of states. The way to deal with this is that M has a finite set of states, and so isomorphic to a standard finite set of the form \mathbb {N}_{<n}, which has computably decidable equality. A more important subtlety is that we need it to be computably decidable whether a given state is an accepting state, and we also need the transition function to be computable; any subset of a finite set is going to be decidable for roughly the same reason—ahead of time, we can enumerate it as a list of numbers in \mathbb {N}_{<n}.
Corollary Closure of regular languages under complementation [018F]
- February 16, 2025
- Jon Sterling
If L is a regular language over an alphabet \Sigma , then so is its complement \Sigma ^\star \setminus L.
Proof.
- February 16, 2025
- Jon Sterling
Let L be a regular language, and so by Kleene’s theorem we have some DFA M satisfying L=\mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}. We define a new DFA \lnot {M} satisfying \mathcal {L}{\mathopen {}\left (\lnot {M}\right )\mathclose {}} = \Sigma ^\star \setminus L by setting \lnot {M} to have the same states, transition function, and starting state as M but with the opposite set of accepting states:
F_{\lnot {M}} = Q_{M}\setminus F_{M}The desired property of \lnot {M} is immediate.
Corollary Closure of regular languages under intersection [018G]
- February 16, 2025
- Jon Sterling
The intersection of two regular languages over a fixed alphabet \Sigma is regular.
Proof.
- February 16, 2025
- Jon Sterling
We fix regular languages L_1,L_2\subseteq \Sigma ^\star and consider the intersection L_1\cap L_2. By De Morgan’s laws, we have:
L_1\cap L_2 = \Sigma ^\star \setminus {\mathopen {}\left ( {\mathopen {}\left ( \Sigma ^\star \setminus L_1 \right )\mathclose {}}\cup {\mathopen {}\left ( \Sigma ^\star \setminus L_2 \right )\mathclose {}} \right )\mathclose {}}Because regular languages are closed under complementation, it suffices to check that {\mathopen {}\left ( \Sigma ^\star \setminus L_1 \right )\mathclose {}}\cup {\mathopen {}\left ( \Sigma ^\star \setminus L_2 \right )\mathclose {}} is regular. Regular languages are, by definition, closed under finite unions, so it remains to check that the complements \Sigma ^\star \setminus L_1 and \Sigma ^\star \setminus L_2 are regular. This follows from our assumption that L_1 and L_2 are regular via closure under complements.
Corollary Decidability of regular expression equivalence [018H]
- February 16, 2025
- Jon Sterling
It is decidable whether two regular expressions R,S over a fixed alphabet \Sigma are equivalent in the sense that \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}.
Proof.
- February 16, 2025
- Jon Sterling
We note that two subsets are equal if and only if the intersection of one with the complement of the other is empty, and vice versa:
\begin {aligned} & \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} \\ &\quad \Leftrightarrow \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\subseteq \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\land \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\subseteq \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} \\ &\quad \Leftrightarrow {\mathopen {}\left ({\mathopen {}\left (\Sigma ^\star \setminus \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\right )\mathclose {}}\cap \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\right )\mathclose {}} =\varnothing = {\mathopen {}\left ({\mathopen {}\left (\Sigma ^\star \setminus \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\right )\mathclose {}}\cap \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\right )\mathclose {}} \end {aligned}We therefore let M and N be two DFAs recognising the language {\mathopen {}\left (\Sigma ^\star \setminus \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}\right )\mathclose {}}\cap \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} and {\mathopen {}\left (\Sigma ^\star \setminus \mathcal {L}{\mathopen {}\left (S\right )\mathclose {}}\right )\mathclose {}}\cap \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} respectively. (These DFAs can be obtained computably using the compilation procedure by which we proved Corollary [018A].) It follows then that \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}}=\mathcal {L}{\mathopen {}\left (S\right )\mathclose {}} if and only if \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}}=\varnothing =\mathcal {L}{\mathopen {}\left (N\right )\mathclose {}}.
So it now remains to check whether there exists any string at all that is accepted by M and likewise for N. We do not have to check all strings over \Sigma , which would be impossible: we observe that the number of states in M bounds the number of transitions required to reach an accepting state, because any loops about a state q can be excised from the path without changing the result. This is a special property of the finite automata that we have studied so far, corresponding to the fact that they have no memory.