(see: http://www.cdf.toronto.edu/~heap/148/F13/Assignments/A2/a2.pdf)
After writing the last couple of sentences above, I realized that regular expressions form an algebraic structure! (I should have realized this sooner, since I am a pure mathematics student.)
The algebraic structure consists of a set S of all regular expressions, along with two binary operations: | : SxS -> S and . : SxS -> S, and one unary operation: * : S -> S. Moreover, the numbers: 0, 1, and e are defined to be regular expressions, and are thus elements of S. Hence, all compositions of the binary operations and unary operations that make sense, map regular expressions 0, 1, e, and any other regular expressions in S to regular expressions in S.
We use infix notation for | and . and postfix notation for *. Also, composition is denoted with parentheses as in ordinary arithmetic and * by convention takes precedence over the other operators as is the case with postfix operations and prefix operations in ordinary arithmetic (such as: negation). Thus, the following is a regular expression:
((1.(1|0)*).1).
The outer parentheses can be removed without rendering the expression ambiguous, as in:
(1) (1.(1|0)*).1,
but we use them by convention in our assignment.
For those who do not know what an algebraic structure is, a familiar one are the integers along with the binary operations of addition and multiplication, and the unary operation of negation. In this algebraic structure (Z, +, x, -), which is a type of ring, the following expression is an integer:
(1x-(1+0))x1.
If we use postfix notation for negation instead of prefix notation, then the expression becomes:
(2) (1x(1+0)-)x1.
Do you see the similarity between (1) and (2)?
Now, by studying the structure of (S, |, ., *) it might be possible to find different identities and properties that the operations defined on S satisfy. Using these identities, we can simplify the code used for matching strings to regular expressions in the regex_match function. But, in order to write down identities between regular expressions we need some notion of "equality" between regular expressions. Thus, we must define an equivalence relation on our algebraic structure and for this equivalence relation to be of any use it must be compatible with the structure of our algebraic structure. Such an equivalence relation is called a congruence relation. Now, what does it mean for two regular expressions to be "equal"? Well, a regular expression r codes for a particular structure that a string can possess. Thus, a regular expression defines a set of strings that all possess the the particular structure coded for by the regular expression, we'll denote this set S(r). Sometimes a string might possess the structure that two different regular expressions code for even though the structures they code for are in general very different and usually not possessed by the same string. For example the binary string '' (the empty string) is in the set S(e) and is also in the set S(1*), however the string '111' is in the set S(1*), but not in the set S(e). It does not make sense to consider two regular expressions r and s to be equal if the sets S(r) and S(s) only have one or a few elements in common. A useful definition of equality between between regular expressions is the following:
Def.: Two regular expressions r and s are equal, denoted r ~ s, iff S(r) = S(s).
This is indeed an equivalence relation, and it is trivial to verify that this is the case because it inherits the necessary properties of an equivalence relation from set equality, which is an equivalence relation.
In our assignment we say a string matches a regular expression iff it possesses the structure coded for by the regular expression. That is, a string x matches a regular expression r iff x is an element of S(r). Thus, an equivalent definition of regular expression equality is:
Def.: Two regular expressions r and s are equal, denoted r ~ s, iff every string that matches r also matches s and vice versa.
From the definitions of r | s, r . s, and r*, where r and s are regular expressions, (refer to the assignment handout hyperlink above) it follows from the way that we have defined regular expression equality that, ~ is a congruence relation. This means that if r ~ s, then in any expression containing r we can replace r with s without changing the string structure that the resulting expression codes for.
One simply identity in the algebra of our regular expressions is that the operation * is idempotent. That is, if r is a regular expression, then
r** ~ r*.
This identity is easy to verify, try it! Moreover, by induction we have that
(3) r*...* ~ r*,
for any number of times that the r on the left is "starred". Therefore, in the code for the function regex_match from our assignment, the case that treats checking if a string matches a regular expression that is starred (potentially any positive integer number of times) only needs to check if it matches the regular expression when it is starred once. This drastically reduces the number of recursive function calls needed in the code in the cases where the regular expression argument contains a regular subexpression that is starred a large number of times. My partner and I used this identity (equation (3)) to our advantage in the programming of our assignment.
I encourage the reader to investigate this algebraic structure; if he or she finds any interesting identities, please comment them. I would be very interested in seeing what you come up with!
Now, by studying the structure of (S, |, ., *) it might be possible to find different identities and properties that the operations defined on S satisfy. Using these identities, we can simplify the code used for matching strings to regular expressions in the regex_match function. But, in order to write down identities between regular expressions we need some notion of "equality" between regular expressions. Thus, we must define an equivalence relation on our algebraic structure and for this equivalence relation to be of any use it must be compatible with the structure of our algebraic structure. Such an equivalence relation is called a congruence relation. Now, what does it mean for two regular expressions to be "equal"? Well, a regular expression r codes for a particular structure that a string can possess. Thus, a regular expression defines a set of strings that all possess the the particular structure coded for by the regular expression, we'll denote this set S(r). Sometimes a string might possess the structure that two different regular expressions code for even though the structures they code for are in general very different and usually not possessed by the same string. For example the binary string '' (the empty string) is in the set S(e) and is also in the set S(1*), however the string '111' is in the set S(1*), but not in the set S(e). It does not make sense to consider two regular expressions r and s to be equal if the sets S(r) and S(s) only have one or a few elements in common. A useful definition of equality between between regular expressions is the following:
Def.: Two regular expressions r and s are equal, denoted r ~ s, iff S(r) = S(s).
This is indeed an equivalence relation, and it is trivial to verify that this is the case because it inherits the necessary properties of an equivalence relation from set equality, which is an equivalence relation.
In our assignment we say a string matches a regular expression iff it possesses the structure coded for by the regular expression. That is, a string x matches a regular expression r iff x is an element of S(r). Thus, an equivalent definition of regular expression equality is:
Def.: Two regular expressions r and s are equal, denoted r ~ s, iff every string that matches r also matches s and vice versa.
From the definitions of r | s, r . s, and r*, where r and s are regular expressions, (refer to the assignment handout hyperlink above) it follows from the way that we have defined regular expression equality that, ~ is a congruence relation. This means that if r ~ s, then in any expression containing r we can replace r with s without changing the string structure that the resulting expression codes for.
One simply identity in the algebra of our regular expressions is that the operation * is idempotent. That is, if r is a regular expression, then
r** ~ r*.
This identity is easy to verify, try it! Moreover, by induction we have that
(3) r*...* ~ r*,
for any number of times that the r on the left is "starred". Therefore, in the code for the function regex_match from our assignment, the case that treats checking if a string matches a regular expression that is starred (potentially any positive integer number of times) only needs to check if it matches the regular expression when it is starred once. This drastically reduces the number of recursive function calls needed in the code in the cases where the regular expression argument contains a regular subexpression that is starred a large number of times. My partner and I used this identity (equation (3)) to our advantage in the programming of our assignment.
I encourage the reader to investigate this algebraic structure; if he or she finds any interesting identities, please comment them. I would be very interested in seeing what you come up with!