Consider the following non-deterministic finite automaton (NFA):

NFA with two states

If you’ve studied automata theory, you likely learnt that NFAs are equivalent to regular expressions, in the sense that for every NFA there is a regular expression that recognizes the same set of strings as the NFA. So, what is an equivalent regular expression for the NFA above?

In this post, I’d like to share a rather peculiar method to compute the answer to such questions by solving a system of linear equations over regular expressions1. I first learnt this method from Dr. Christian Urban’s course on compilers and formal languages at KCL on exchange.

Let r0r_0 be a regular expression that matches strings that end in state Q0Q_0 when you run them through the automaton, and similarly let r1r_1 be a regular expression that matches strings ending in state Q1Q_1. Although we don’t know what r0r_0 and r1r_1 are yet—if we did, we’d be done!—we can try to write down some constraints that they ought to satisfy. For instance, looking at the NFA diagram, the only way to get to state Q0Q_0 is if we haven’t seen any characters yet (an empty string), or if we were previously in state Q1Q_1 and saw the character bb. In other words, the regular expression r0r_0 should match either the empty string, or r1r_1 followed by the character bb.

Let’s now agree on two (seemingly unmotivated) conventions for writing regular expressions: instead of using | to denote alternation as in r1∣r2r_1 |r_2, we will instead write r1+r2r_1 + r_2, and additionally we will use 1\mathbf{1} to denote the regular expression matching the empty string. We’ll also agree to say that two regular expressions are equal if they match the same set of strings.

Using these strange conventions, we get

r0=1+r1b(1) r_0 = \mathbf{1} + r_1 b \tag{1}

which says that r0r_0 matches either the empty string, or r1r_1 followed by the character bb, as required. Similarly, for r1r_1, we have the equation

r1=r1a+r0a.(2) r_1 = r_1 a + r_0 a. \tag{2}

Let’s now throw caution to the wind completely and attempt to solve these equations algebraically, ignoring for the moment that we haven’t the faintest clue what manipulations on these “regular expression equations” are legal or what laws they obey, if any.

By some questionable manipulations on equation (2), we get

r1−r1a=r0a  ⟹  (1−a)r1=r0a  ⟹  r1=r0a(1−a)−1. r_1 - r_1 a = r_0 a \implies (\mathbf{1} - a)r_1 = r_0 a \implies r_1 = r_0 a ({\mathbf{1} - a})^{-1}.

Everyone and their mother knows that (1−a)−1(\mathbf{1} - a)^{-1} is just the sum of the infinite geometric series 1+a+a2+⋯\mathbf{1} + a + a^2 + \cdots. Alas, we can’t have a regular expression of infinite length, but recalling that ++ means alternation, the expression 1+a+a2+⋯\mathbf{1} + a + a^2 + \cdots seems like it matches all the strings that are the character aa repeated. But that’s just the regular expression a∗a^*! So:

r1=r0aa∗.(3) r_1 = r_0 a a^*. \tag{3}

Now substitute (3) into (1) to get r0=1+r0aa∗br_0 = \mathbf{1} + r_0 a a^* b. By applying our questionable geometric series trick again,

r0=(aa∗b)∗. r_0 = (a a^* b)^*.

Substituting into (3) yields

r1=(aa∗b)∗aa∗ r_1 = \boxed{(a a^* b)^* a a^*}

and since Q1Q_1 is the only accepting state of the automaton, this regexp should match exactly the strings that are accepted by the NFA. And it truly does! This is the right answer to the original problem (!)

What the hell just happened?

A suspicious algorithm

Summarizing what we just did, here’s our tried-and-true method to convert NFAs to equivalent regular expressions: if the NFA has states Q1,...,QnQ_1, ..., Q_n, let r1,...,rnr_1, ..., r_n denote indeterminate regular expressions that match the strings ending in the respective states. Write down nn equations of the form ri=fi(r1,...,rn)r_i = f_i(r_1, ..., r_n) relating the regular expressions to one another according to the transitions of the NFA, using the notation 1\mathbf{1} and ++ as defined before.

Then, simply solve this system of nn equations in nn variables by substitution, using the rule r=q+rs  ⟹  r=qs∗r = q + r s \implies r = q s^* when needed (the “geometric series inverse trick”). The answer is given by adding up (i.e., taking an alternation of) the regular expressions corresponding to the accepting states of the NFA.


It turns out that this method does work in general! However, if you’re like me, you are probably scratching your head at this point: clearly these equations have to be interpreted in some kind of underlying algebraic structure on regular expressions. In the remainder of this post, I thus want to answer the following

Question. What is the algebraic structure on regular expressions that allows us to make sense of the equations above? How can we be sure that a unique solution to linear systems formed by the method above exists?

I’ll mostly give intuition, definitions, and statements of results instead of proofs—partly because I’m lazy, and partly because I think the proofs are not really enlightening, but the definitions are.

Regular expressions revisited

To start, we should clarify what we mean by a regular expression: only ‘basic’ features such as sequencing, alternation, and repetition (*) are permitted; fancy stuff like capturing groups, backreferences, and the like are all out of scope. We define regular expressions formally as a recursive datatype.

Definition. A regular expression (regexp) is one of the following:

  • 1\mathbf{1}, matching the empty string
  • a literal symbol c∈Σc \in \Sigma, where Σ\Sigma is a fixed alphabet of valid symbols
  • a sequence r1⋅r2r_1 \cdot r_2 of smaller two regexps r1,r2r_1, r_2; often we will omit the ⋅\cdot symbol and just juxtapose the regexps
  • an alternation r1+r2r_1 + r_2
  • or a repetition r∗r^*.

(There’s in fact one more case, which we’ll get to later—in particular there is also such as a thing as 0\mathbf{0}; see the first paragraph of the next section.)

We declare two regexps to be equal when they match the same set of strings. Formally, we define recursively a function L(r)\mathcal{L}(r) that returns the set of strings that rr matches: L(1)={ϵ}\mathcal{L}(\mathbf{1}) = \{\epsilon\}, L(r1+r2)=L(r1)∪L(r2)\mathcal{L}(r_1 + r_2) = \mathcal{L}(r_1) \cup \mathcal{L(r_2)}, etc. (What should the definition for L(r1⋅r2)\mathcal{L}(r_1 \cdot r_2) be?2) Two regexps are then equal if L(r1)=L(r2)\mathcal{L}(r_1) = \mathcal{L}(r_2).

As you have probably gleaned, the object 1\mathbf{1} and the operations ++, ⋅\cdot behave somewhat like their real number counterparts. Here’s a non-exhaustive list of nice properties:

  • addition is commutative: a+b=b+aa + b = b + a, since both match either bb or aa.
  • 1\mathbf{1} is the identity with respect to multiplication: 1⋅a=a\mathbf{1} \cdot a = a, since the left side means “match 1\mathbf{1}, then match aa”, and 1\mathbf{1} matches the empty string.
  • multiplication is associative: (a⋅b)⋅c=a⋅(b⋅c)(a \cdot b) \cdot c = a \cdot (b \cdot c).
  • multiplication distributes over a sum: a⋅(b+c)=ab+aba\cdot (b + c) = ab + ab, since the left side means “match aa, then either bb or cc, which is the same as matching either abab or acac”.

There are some oddities, of course. For instance:

  • everything is idempotent with respect to ++: we have r+r=rr + r = r for all regexps rr.
  • we do not have multiplicative inverses in general.3
  • multiplication is not commutative: in general r1⋅r2≠r2⋅r1r_1 \cdot r_2 \ne r_2 \cdot r_1, since r1⋅r2r_1 \cdot r_2 means to match r1r_1 followed by r2r_2 and the order matters.

With these properties and not-so-nice properties in mind, let’s ask again: what kind of algebraic structure do regular expressions possess?

Rings, semirings, and Kleene algebras

Perhaps you suspect at this point that the set of regular expressions (R,+,⋅)(R, +, \cdot) forms a (non-commutative) ring. One obvious issue is that we don’t have an identity for addition; note that 1\mathbf{1} is not neutral with respect to addition, since 1+a\mathbf{1} + a matches both the empty string and aa, not just aa. This issue is easily remedied by adding a new object 0\mathbf{0} that matches no strings at all to the definition of regular expression. One can check that 0\mathbf{0} is indeed neutral with respect to addition and possesses various properties that something called zero ought to.

Unfortunately, even with 0\mathbf{0} the regular expressions do not have a ring structure for entirely trivial reasons. Indeed, additive inverses do not exist in general: given a regular expression rr, what is −r-r? (One might be tempted to define it as the regular expression matching the complement of the language matched by rr, but unfortunately this does not play well with our existing definitions.4)

Thus (R,+,⋅)(R, +, \cdot) is not a ring, since additive inverses do not exist. However, one can actually check that the lack of additive inverses is the only obstruction to (R,+,⋅)(R, +, \cdot) being a ring. There is a name for structures that are almost rings but lack only additive inverses: semirings.

Definition (semiring). A semiring (R,+,⋅)(R, +, \cdot) satisfies all the ring axioms, except that additive inverses need not exist.

Proposition. The set of regular expressions RR equipped with alternation as addition ++ and sequencing as multiplication ⋅\cdot forms a semiring.

But this does not explain everything. In particular, we have not said how the star operation r∗r^* fits into the algebraic structure. If you recall how we “solved” the equation r1=r1a+r0ar_1 = r_1 a + r_0 a by heuristically writing “r1(1−a)=r0ar_1 (1 - a) = r_0 a” and then using a “geometric series expansion” to get r1=r0aa∗r_1 = r_0 a a^*, it seems that the star operation acts as a kind of pseudoinverse.

The notion of a Kleene algebra extends the notion of a semiring to include the ∗* operation.

Definition (Kleene algebra). A Kleene algebra (K,+,⋅,∗)(K, +, \cdot, *) is an idempotent semiring (K,+,⋅)(K, +, \cdot) with a unary operation ∗* such that

  • for all a∈Ka \in K, we have 1+a(a∗)≤a∗1 + a(a^*) \le a^* and 1+(a∗)a≤a∗1 + (a^*)a \le a^*,
  • for all a,x∈Ka, x \in K such that ax≤xax \le x, then a∗x≤xa^*x \le x and x(a∗)≤xx(a^*) \le x,

where x≤yx \le y means x+y=yx + y = y.

For intuition about the definition of ≤\le, observe that generally we may interpret ++ in a semiring as a kind of union operation, and so x+y=yx + y = y is equivalent to yy “including” xx. In the setting of regular expressions, we see that r1+r2=r2r_1 + r_2 = r_2 implies that r2r_2 matches all the strings that r1r_1 does (and potentially more).

Proposition. The set of regular expressions (R,+,⋅,∗)(R, +, \cdot, *) is a Kleene algebra.

(Exercise: interpret the two additional axioms concerning ∗* in the context of regular expressions. Why do they make sense?)

In fact, regular expressions are the prototypical example of a Kleene algebra; that is, Kleene algebras were introduced to study regular expressions!

In any case, from the defining axioms of a Kleene algebra, we can deduce the following

Lemma (Arden’s lemma). In a Kleene algebra, ba∗ba^* is the least solution of x=xa+bx = xa + b with respect to ≤\le.

which (under a mild technical condition) validates our original rule r=p+rs  ⟹  r=ps∗r = p + rs \implies r = ps^* for regular expressions.


Returning to the motivating question of this post—what algebraic structure allows us to make sense of “linear systems in regular expressions”?—one answer is thus that the regular expressions (R,+,⋅,∗)(R, +, \cdot, *) form a Kleene algebra, which is a special case of a semiring. In a Kleene algebra, we may of course solve by substitution and moreover the rule r=p+rs  ⟹  r=ps∗r = p + rs \implies r = ps^* is valid (under a mild technical condition) and known more generally as Arden’s lemma.

Is that the whole story? We haven’t discussed existence and uniqueness of solutions yet, but one can imagine that with the formal definitions above it is not too hard to slog through a proof.

One element of the Kleene algebra viewpoint that I personally find unsatisfying is that we originally “derived” the rule r=p+rs  ⟹  r=ps∗r = p + r s \implies r = ps^* by heuristically manipulating r−rs=r(1−s)=pr - rs = r(1 - s) = p, then multiplying by the inverse of 1−s1-s and using a geometric series expansion to get

r=p(1−s)−1=p(1+s+s2+⋯ )=ps∗.r = p(1-s)^{-1} = p(1 + s + s^2 + \cdots) = ps^*.

From the viewpoint of Kleene algebras, even though the end result is justified by Arden’s lemma and ∗* can be understood as a kind of pseudoinverse, the intermediate steps are nothing more than a heuristic and cannot be justified formally. One would be excused for dismissing the argument above as nothing more than heuristic thinking that chances upon the right answer—but in fact we can make this argument fully rigorous in the right setting. In particular, by interpreting regular expressions as formal noncommutative power series or formal power series over words, the “inverse” of a regular expression exists in a larger ring. In the last section of this post I will outline this viewpoint and sketch nice proofs of existence and uniqueness of solutions.

Formal power series over words

We build up to the notion of a formal noncommutative power series from more familiar definitions.

Definition (formal power series in one variable). Let RR be a ring. Then R[[X]]R[[X]] denotes the ring of formal power series ∑k=0∞anXn\sum_{k=0}^\infty a_n X^n in the indeterminate XX with coefficients in RR.

Definition (formal power series in multiple commuting variables). Let RR be a ring. Then R[[X1,...,Xr]]R[[X_1, ..., X_r]] denotes the ring of formal power series ∑k1,…,kr≥0akX1k1⋯Xrkr\sum_{k_1, \dots, k_r \ge 0} a_\mathbf{k} X_1^{k_1} \cdots X_r^{k_r} in rr commuting indeterminates X1,...,XrX_1, ..., X_r with coefficients in RR.

By commuting indeterminates, we mean that we can freely interchange the order in a product: X1X2X1=X1X1X2=X12X2X_1 X_2 X_1 = X_1 X_1 X_2 = X_1^2 X_2. This allows us to rewrite any product of indeterminates in the canonical form X1k1⋯XrkrX_1^{k_1} \cdots X_r^{k_r}.

If we drop this condition, and instead adjoin non-commutative indeterminates, we naturally obtain the definition of formal noncommutative power series. We indicate noncommutativity by using angled brackets instead of square brackets: that is, in R[[X1,X2]]R[[X_1, X_2]] the indeterminates X1X_1 and X2X_2 commute, whereas in R⟨⟨X1,X2⟩⟩R\langle\langle X_1, X_2\rangle\rangle they do not.

Let’s think about what the power series living in R⟨⟨X1,X2⟩⟩R\langle\langle X_1, X_2\rangle\rangle look like. In particular, what form do individual terms take? In the commutative case each term had the form ak1k2X1k1X2k2a_{k_1k_2} X_1^{k_1}X_2^{k_2}, which does not distinguish between (say) 5x1x2x1x15 x_1 x_2 x_1 x_1 and 5x13x25 x_1^3 x_2. In the noncommutative case these terms are distinct, so we should instead think about terms having the form

cXw1Xw2⋯Xwm c X_{w_1} X_{w_2} \cdots X_{w_m}

where w1,…,wmw_1, \dots, w_m is a finite sequence of 1s and 2s. The term x1x2x1x1x_1 x_2 x_1 x_1 corresponds to (1,2,1,1)(1, 2, 1, 1), whereas x13x2x_1^3 x_2 corresponds to (1,1,1,2)(1, 1, 1, 2), which is a different sequence.

In this way, we see that the power series living in R⟨⟨X1,…,Xm⟩⟩R\langle\langle X_1, \dots, X_m\rangle\rangle are naturally indexed by words (finite sequences) where each symbol is in {1,...,m}=:Σ\{1,...,m\} =: \Sigma, of the form ∑ω∈Σ∗aωXω\sum_{\omega \in \Sigma*} a_\omega X_\omega. This leads us to the definition

Definition (formal power series on words). Let RR be a ring and Σ\Sigma be a finite alphabet of symbols. Then R⟨⟨Σ⟩⟩R\langle\langle \Sigma \rangle\rangle denotes the ring of formal noncommutative power series ∑ω∈Σ∗aωXω\sum_{\omega \in \Sigma^*} a_\omega X_\omega.

and as a special case

Definition (formal power series in multiple noncommuting variables). Let RR be a ring. Then R⟨⟨X1,…,Xn⟩⟩R\langle\langle X_1, \dots, X_n \rangle \rangle denotes the ring of formal power series ∑ω∈{1,…,n}∗XωXω\sum_{\omega \in \{1,\dots,n\}^*} X_\omega X_\omega.

It took us a bit to arrive at the right definitions, but I hope that the scenic route we took above gives some intuition for why power series on words is a natural non-commutative generalization of regular power series.

Remark. If you really don’t like to reason about power series on words, you can also think of R⟨⟨Σ⟩⟩R\langle\langle \Sigma \rangle\rangle as the set of functions f:Σ∗→Rf : \Sigma^* \to R assigning each word an element of RR. (Why is this equivalent to our definition?)

Regular expressions are just formal series

You may rightfully be wondering what the point of defining power series on words is. Well, let’s write down a regular expression:

r=a(ab+ac). r = a(a b + a c).

Expand this out as r=aab+aacr = a a b + a a c. The key insight is that we can think of this as a noncommutative power series in indeterminates aa, bb, cc over Z\mathbb{Z}, where the coefficient on aaba a b is 11 and the coefficient on aaca a c is also 11!

Are the coefficients always one or zero? Not quite: consider ab+(c+a)ba b + (c + a)b; it expands to ab+cb+ab=2ab+cba b + c b + a b = 2 a b + cb. The coefficient 22 on abab counts the number of ways for the regular expression to match abab: see ab+(c+a)b{\color{red}ab} + (c + {\color{red}a}){\color{red}b}.

What is the analogue of the star operation in Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma\rangle\rangle? It seems that we’re a bit stuck now, but let’s recall the heuristic geometric series argument we made earlier: we have (1−r)−1=1+r+r2+⋯=r∗(1-r)^{-1} = 1 + r + r^2 + \cdots = r^*, where the last equality is justified since 1+r+r2+⋯1 + r + r^2 + \cdots matches any repetition of rr’s, which is precisely the definition of r∗r^*.

So perhaps the interpretation of r∗r^* as a formal power series should be (1−[r])−1(1-[r])^{-1}, where [r][r] interprets rr as a power series in Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma\rangle\rangle. The beauty is that we can take inverses in the power series ring Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma \rangle\rangle, as long as the constant term of the power series is a unit!

Theorem. Let RR be a ring and let f∈R⟨⟨Σ⟩⟩f \in R\langle\langle \Sigma \rangle\rangle. Then f−1f^{-1} exists if and only if the constant term of ff is a unit in RR.

Corollary. Let RR be a ring and suppose that g∈R⟨⟨Σ⟩⟩g \in R\langle\langle \Sigma \rangle\rangle has no constant term. Then (1−g)−1(1 - g)^{-1} exists.

Proof. The constant term of 1−g1 - g is 1∈R×1 \in R^\times.

Let’s define the correspondence between regular expressions and formal power series formally now.

Definition (power series interpretation of regexp). Let RR denote the semiring of regular expressions over Σ\Sigma. Recall that Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma \rangle\rangle denotes the ring of formal power series on words over Σ\Sigma.

Define the power series interpretation of a regular expression rr, denoted [r][r], by

  • [0]=0[\mathbf{0}] = 0
  • [1]=1[\mathbf{1}] = 1
  • [r1+r2]=[r1]+[r2][r_1 + r_2] = [r_1] + [r_2]
  • [r1r2]=[r1][r2][r_1 r_2] = [r_1] [r_2]
  • [r∗]=(1−[r])−1[r^*] = (1 - [r])^{-1} whenever rr does not match the empty string.

Here [⋅][\cdot] is a partial function R→Z⟨⟨Σ⟩⟩R \to \mathbb{Z}\langle\langle \Sigma \rangle\rangle.

The condition rr does not match the empty string in [r∗][r^*] guarantees that [r][r] has no constant term and hence 1−[r]1 - [r] is invertible. Intuitively, when rr matches the empty string, we should not expect r∗r^* to have a well-defined power series interpretation since there are infinitely many ways for r∗r^* match the empty string, hence the constant term of [r∗][r^*] must be ∞\infty, which is not in Z\mathbb{Z}.


In the power series interpretation of regular expressions, the heuristic argument that we originally made has a completely rigorous interpretation in Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma \rangle\rangle, since subtraction is well-defined and the geometric series expansion (1−r)−1=1+r+r2+⋯(1-r)^{-1} = 1 + r + r^2 + \cdots is valid whenever rr has no constant term!

Working in Z⟨⟨Σ⟩⟩\mathbb{Z}\langle\langle \Sigma \rangle\rangle also permits us to prove that the system of linear equations arising from NFA to regexp conversion has a solution. Indeed, take a ϵ\epsilon-free NFA with states Q1,…,QnQ_1, \dots, Q_n, initial states II and accepting states FF. Define a row vector u\mathbf{u} by ui=1u_i = 1 if Qi∈IQ_i \in I and ui=0u_i = 0 otherwise. Also define a n×nn \times n matrix by

Aji=∑c∈Σ : exists transition Qj→cQic. \textstyle A_{ji} = \sum_{c \in \Sigma\ : \ \text{exists transition } Q_j \xrightarrow{c} Q_i} c.

For instance, if the alphabet is {a,b}\{a, b\} and the NFA contains transitions Q1→aQ2Q_1 \xrightarrow{a} Q_2 and Q2→bQ2Q_2 \xrightarrow{b} Q_2, then A12=a+bA_{12} = a + b.

Each entry of AA is a nonempty sum of literal symbols (as there are no ϵ\epsilon-transitions), so AA has no constant term. Writing r=(r1,…,rn)\mathbf{r} = (r_1, \dots, r_n), the equations ri=ui+∑jrjAjir_i = u_i + \sum_j r_j A_{ji} produced by our method become the single matrix equation

r=u+rA. \mathbf{r} = \mathbf{u} + \mathbf{r} A.

Theorem (existence and uniqueness). Let AA and u\mathbf{u} be as above. Then the system r=u+rA\mathbf{r} = \mathbf{u} + \mathbf{r}A has a unique solution r∈Z⟨⟨Σ⟩⟩n\mathbf{r} \in \mathbb{Z}\langle\langle \Sigma \rangle\rangle^n, given by

r=u(In×n−A)−1=u(Id+A+A2+⋯ ). \mathbf{r} = \mathbf{u}(I_{n \times n} - A)^{-1} = \mathbf{u}(\mathrm{Id} + A + A^2 + \cdots).

Moreover, the coefficient of a word ww in rir_i is the number of paths in the NFA that start in a state of II, read ww, and end in QiQ_i. In particular, the words with nonzero coefficient in ∑Qi∈Fri\sum_{Q_i \in F} r_i are exactly the words accepted by the NFA.

TODO some subleties (proper regexps, does the answer represent a valid regexp, provenance of these ideas, conclusion)


  1. Don’t worry if you haven’t a clue what it means to solve a linear system over regular expressions. I had no idea what was going on under the hood initially either! ↩︎

  2. A bit of pondering leads to the definition {s1s2:s1∈L(r1)L(r2)}\{s_1 s_2 : s_1 \in \mathcal{L}(r_1) \mathcal{L}(r_2)\}, where s1s2s_1 s_2 denotes the concatenation of strings s1s_1 and s2s_2. ↩︎

  3. Consider the regexp aa matching the literal character aa. Then, if a−1a^{-1} is a multiplicative inverse of aa, we must have

    L(aa−1)={s1s2:s1∈L(a),s2∈L(a−1)}\mathcal{L}(aa^{-1}) = \{s_1 s_2 : s_1 \in \mathcal{L}(a), s_2 \in \mathcal{L}(a^{-1})\}

    and in particular this set cannot equal L(1)={ϵ}\mathcal{L}(\mathbf{1}) = \{\epsilon\}: the displayed set is either empty, or contains one or more strings starting with character aa. ↩︎

  4. Every regular expression rr is idempotent with respect to ++, since matching either rr or rr is the same as matching rr; that is, r+r=rr + r = r. If additive inverses exist, adding −r-r then gives r=0r = \mathbf{0}. But rr was arbitrary, so this says that the only regular expression is 0\mathbf{0}—no good! ↩︎