532
|
1 |
% Chapter Template
|
|
2 |
|
|
3 |
\chapter{Finiteness Bound} % Main chapter title
|
|
4 |
|
|
5 |
\label{Finite}
|
|
6 |
% In Chapter 4 \ref{Chapter4} we give the second guarantee
|
|
7 |
%of our bitcoded algorithm, that is a finite bound on the size of any
|
|
8 |
%regex's derivatives.
|
|
9 |
|
|
10 |
In this chapter we give a guarantee in terms of time complexity:
|
|
11 |
given a regular expression $r$, for any string $s$
|
|
12 |
our algorithm's internal data structure is finitely bounded.
|
543
|
13 |
Note that it is not immediately obvious that $\llbracket \bderssimp{r}{s} \rrbracket$ (the internal
|
|
14 |
data structure used in our $\blexer$)
|
|
15 |
is bounded by a constant $N_r$, where $N$ only depends on the regular expression
|
|
16 |
$r$, not the string $s$.
|
|
17 |
When doing a time complexity analysis of any
|
|
18 |
lexer/parser based on Brzozowski derivatives, one needs to take into account that
|
|
19 |
not only the "derivative steps".
|
|
20 |
|
|
21 |
%TODO: get a grpah for internal data structure growing arbitrary large
|
|
22 |
|
|
23 |
|
532
|
24 |
To obtain such a proof, we need to
|
|
25 |
\begin{itemize}
|
|
26 |
\item
|
|
27 |
Define an new datatype for regular expressions that makes it easy
|
|
28 |
to reason about the size of an annotated regular expression.
|
|
29 |
\item
|
|
30 |
A set of equalities for this new datatype that enables one to
|
|
31 |
rewrite $\bderssimp{r_1 \cdot r_2}{s}$ and $\bderssimp{r^*}{s}$ etc.
|
|
32 |
by their children regexes $r_1$, $r_2$, and $r$.
|
|
33 |
\item
|
|
34 |
Using those equalities to actually get those rewriting equations, which we call
|
|
35 |
"closed forms".
|
|
36 |
\item
|
|
37 |
Bound the closed forms, thereby bounding the original
|
|
38 |
$\blexersimp$'s internal data structures.
|
|
39 |
\end{itemize}
|
|
40 |
|
|
41 |
\section{the $\mathbf{r}$-rexp datatype and the size functions}
|
|
42 |
|
|
43 |
We have a size function for bitcoded regular expressions, written
|
|
44 |
$\llbracket r\rrbracket$, which counts the number of nodes if we regard $r$ as a tree
|
|
45 |
|
|
46 |
\begin{center}
|
|
47 |
\begin{tabular}{ccc}
|
543
|
48 |
$\llbracket _{bs}\ONE \rrbracket$ & $\dn$ & $1$\\
|
|
49 |
$\llbracket \ZERO \rrbracket$ & $\dn$ & $1$ \\
|
|
50 |
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket$ & $\dn$ & $\llbracket r_1 \rrbracket + \llbracket r_2 \rrbracket + 1$\\
|
|
51 |
$\llbracket _{bs}\mathbf{c} \rrbracket $ & $\dn$ & $1$\\
|
|
52 |
$\llbracket _{bs}\sum as \rrbracket $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket)\; as + 1$\\
|
|
53 |
$\llbracket _{bs} a^* \rrbracket $ & $\dn$ & $\llbracket a \rrbracket + 1$
|
|
54 |
\end{tabular}
|
|
55 |
\end{center}
|
|
56 |
|
|
57 |
\noindent
|
|
58 |
Similarly there is a size function for plain regular expressions:
|
|
59 |
\begin{center}
|
|
60 |
\begin{tabular}{ccc}
|
|
61 |
$\llbracket \ONE \rrbracket_p$ & $\dn$ & $1$\\
|
|
62 |
$\llbracket \ZERO \rrbracket_p$ & $\dn$ & $1$ \\
|
|
63 |
$\llbracket r_1 \cdot r_2 \rrbracket_p$ & $\dn$ & $\llbracket r_1 \rrbracket_p + \llbracket r_2 \rrbracket_p + 1$\\
|
|
64 |
$\llbracket \mathbf{c} \rrbracket_p $ & $\dn$ & $1$\\
|
|
65 |
$\llbracket r_1 \cdot r_2 \rrbracket_p $ & $\dn$ & $\llbracket r_1 \rrbracket_p \; + \llbracket r_2 \rrbracket_p + 1$\\
|
|
66 |
$\llbracket a^* \rrbracket_p $ & $\dn$ & $\llbracket a \rrbracket_p + 1$
|
532
|
67 |
\end{tabular}
|
|
68 |
\end{center}
|
|
69 |
|
543
|
70 |
\noindent
|
|
71 |
The idea of obatining a bound for $\llbracket \bderssimp{a}{s} \rrbracket$
|
|
72 |
is to get an equivalent form
|
|
73 |
of something like $\llbracket \bderssimp{a}{s}\rrbracket = f(a, s)$, where $f(a, s)$
|
|
74 |
is easier to estimate than $\llbracket \bderssimp{a}{s}\rrbracket$.
|
|
75 |
We notice that while it is not so clear how to obtain
|
|
76 |
a metamorphic representation of $\bderssimp{a}{s}$ (as we argued in chapter \ref{Bitcoded2},
|
|
77 |
not interleaving the application of the functions $\backslash$ and $\bsimp{\_}$
|
|
78 |
in the order as our lexer will result in the bit-codes dispensed differently),
|
|
79 |
it is possible to get an slightly different representation of the unlifted versions:
|
|
80 |
$ (\bderssimp{a}{s})_\downarrow = (\erase \; \bsimp{a \backslash s})_\downarrow$.
|
|
81 |
This suggest setting the bounding function $f(a, s)$ as
|
|
82 |
$\llbracket (a \backslash s)_\downarrow \rrbracket_p$, the plain size
|
|
83 |
of the erased annotated regular expression.
|
|
84 |
This requires the the regular expression accompanied by bitcodes
|
|
85 |
to have the same size as its plain counterpart after erasure:
|
532
|
86 |
\begin{center}
|
543
|
87 |
$\asize{a} \stackrel{?}{=} \llbracket \erase(a)\rrbracket_p$.
|
532
|
88 |
\end{center}
|
543
|
89 |
\noindent
|
|
90 |
But there is a minor nuisance:
|
|
91 |
the erase function unavoidbly messes with the structure of the regular expression,
|
|
92 |
due to the discrepancy between annotated regular expression's $\sum$ constructor
|
|
93 |
and plain regular expression's $+$ constructor having different arity.
|
532
|
94 |
\begin{center}
|
|
95 |
\begin{tabular}{ccc}
|
543
|
96 |
$\erase \; _{bs}\sum [] $ & $\dn$ & $\ZERO$\\
|
|
97 |
$\erase \; _{bs}\sum [a]$ & $\dn$ & $a$\\
|
|
98 |
$\erase \; _{bs}\sum a :: as$ & $\dn$ & $a + (\erase \; _{[]} \sum as)\quad \text{if $as$ length over 1}$
|
532
|
99 |
\end{tabular}
|
|
100 |
\end{center}
|
543
|
101 |
\noindent
|
532
|
102 |
An alternative regular expression with an empty list of children
|
543
|
103 |
is turned into a $\ZERO$ during the
|
532
|
104 |
$\erase$ function, thereby changing the size and structure of the regex.
|
543
|
105 |
Therefore the equality in question does not hold.
|
532
|
106 |
These will likely be fixable if we really want to use plain $\rexp$s for dealing
|
|
107 |
with size, but we choose a more straightforward (or stupid) method by
|
|
108 |
defining a new datatype that is similar to plain $\rexp$s but can take
|
|
109 |
non-binary arguments for its alternative constructor,
|
|
110 |
which we call $\rrexp$ to denote
|
|
111 |
the difference between it and plain regular expressions.
|
|
112 |
\[ \rrexp ::= \RZERO \mid \RONE
|
|
113 |
\mid \RCHAR{c}
|
|
114 |
\mid \RSEQ{r_1}{r_2}
|
|
115 |
\mid \RALTS{rs}
|
|
116 |
\mid \RSTAR{r}
|
|
117 |
\]
|
|
118 |
For $\rrexp$ we throw away the bitcodes on the annotated regular expressions,
|
|
119 |
but keep everything else intact.
|
|
120 |
It is similar to annotated regular expressions being $\erase$-ed, but with all its structure preserved
|
|
121 |
We denote the operation of erasing the bits and turning an annotated regular expression
|
|
122 |
into an $\rrexp{}$ as $\rerase{}$.
|
|
123 |
\begin{center}
|
|
124 |
\begin{tabular}{lcl}
|
543
|
125 |
$\rerase{\ZERO}$ & $\dn$ & $\RZERO$\\
|
|
126 |
$\rerase{_{bs}\ONE}$ & $\dn$ & $\RONE$\\
|
|
127 |
$\rerase{_{bs}\mathbf{c}}$ & $\dn$ & $\RCHAR{c}$\\
|
|
128 |
$\rerase{_{bs}r_1\cdot r_2}$ & $\dn$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
|
|
129 |
$\rerase{_{bs}\sum as}$ & $\dn$ & $\RALTS{\map \; \rerase{\_} \; as}$\\
|
|
130 |
$\rerase{_{bs} a ^*}$ & $\dn$ & $\rerase{a}^*$
|
532
|
131 |
\end{tabular}
|
|
132 |
\end{center}
|
543
|
133 |
\noindent
|
|
134 |
$\rrexp$ give the exact correspondence between an annotated regular expression
|
|
135 |
and its (r-)erased version:
|
|
136 |
\begin{lemma}
|
|
137 |
$\rsize{\rerase a} = \asize a$
|
|
138 |
\end{lemma}
|
554
|
139 |
\noindent
|
543
|
140 |
This does not hold for plain $\rexp$s.
|
|
141 |
|
532
|
142 |
Similarly we could define the derivative and simplification on
|
|
143 |
$\rrexp$, which would be identical to those we defined for plain $\rexp$s in chapter1,
|
|
144 |
except that now they can operate on alternatives taking multiple arguments.
|
|
145 |
|
|
146 |
\begin{center}
|
|
147 |
\begin{tabular}{lcr}
|
543
|
148 |
$(\RALTS{rs})\; \backslash c$ & $\dn$ & $\RALTS{\map\; (\_ \backslash c) \;rs}$\\
|
532
|
149 |
(other clauses omitted)
|
543
|
150 |
With the new $\rrexp$ datatype in place, one can define its size function,
|
|
151 |
which precisely mirrors that of the annotated regular expressions:
|
|
152 |
\end{tabular}
|
|
153 |
\end{center}
|
|
154 |
\noindent
|
|
155 |
\begin{center}
|
|
156 |
\begin{tabular}{ccc}
|
|
157 |
$\llbracket _{bs}\ONE \rrbracket_r$ & $\dn$ & $1$\\
|
|
158 |
$\llbracket \ZERO \rrbracket_r$ & $\dn$ & $1$ \\
|
|
159 |
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket_r$ & $\dn$ & $\llbracket r_1 \rrbracket_r + \llbracket r_2 \rrbracket_r + 1$\\
|
|
160 |
$\llbracket _{bs}\mathbf{c} \rrbracket_r $ & $\dn$ & $1$\\
|
|
161 |
$\llbracket _{bs}\sum as \rrbracket_r $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket_r)\; as + 1$\\
|
|
162 |
$\llbracket _{bs} a^* \rrbracket_r $ & $\dn$ & $\llbracket a \rrbracket_r + 1$
|
532
|
163 |
\end{tabular}
|
|
164 |
\end{center}
|
543
|
165 |
\noindent
|
532
|
166 |
|
543
|
167 |
\subsection{Lexing Related Functions for $\rrexp$}
|
|
168 |
Everything else for $\rrexp$ will be precisely the same for annotated expressions,
|
|
169 |
except that they do not involve rectifying and augmenting bit-encoded tokenization information.
|
|
170 |
As expected, most functions are simpler, such as the derivative:
|
|
171 |
\begin{center}
|
|
172 |
\begin{tabular}{@{}lcl@{}}
|
|
173 |
$(\ZERO)\,\backslash_r c$ & $\dn$ & $\ZERO$\\
|
|
174 |
$(\ONE)\,\backslash_r c$ & $\dn$ &
|
|
175 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
176 |
\ONE\;\textit{else}\;\ZERO$\\
|
|
177 |
$(\sum \;\textit{rs})\,\backslash_r c$ & $\dn$ &
|
|
178 |
$\sum\;(\textit{map} \; (\_\backslash_r c) \; rs )$\\
|
|
179 |
$(r_1\cdot r_2)\,\backslash_r c$ & $\dn$ &
|
|
180 |
$\textit{if}\;\textit{rnullable}\,r_1$\\
|
|
181 |
& &$\textit{then}\;\sum\,[(r_1\,\backslash_r c)\cdot\,r_2,$\\
|
|
182 |
& &$\phantom{\textit{then},\;\sum\,}((r_2\,\backslash_r c))]$\\
|
|
183 |
& &$\textit{else}\;\,(r_1\,\backslash_r c)\cdot r_2$\\
|
|
184 |
$(r^*)\,\backslash_r c$ & $\dn$ &
|
|
185 |
$( r\,\backslash_r c)\cdot
|
|
186 |
(_{[]}r^*))$
|
|
187 |
\end{tabular}
|
|
188 |
\end{center}
|
|
189 |
\noindent
|
|
190 |
The simplification function is simplified without annotation causing superficial differences.
|
|
191 |
Duplicate removal without an equivalence relation:
|
532
|
192 |
\begin{center}
|
|
193 |
\begin{tabular}{lcl}
|
543
|
194 |
$\rdistinct{[]}{rset} $ & $\dn$ & $[]$\\
|
|
195 |
$\rdistinct{r :: rs}{rset}$ & $\dn$ & $\textit{if}(r \in \textit{rset}) \; \textit{then} \; \rdistinct{rs}{rset}$\\
|
532
|
196 |
& & $\textit{else}\; r::\rdistinct{rs}{(rset \cup \{r\})}$
|
|
197 |
\end{tabular}
|
|
198 |
\end{center}
|
|
199 |
%TODO: definition of rsimp (maybe only the alternative clause)
|
543
|
200 |
\noindent
|
554
|
201 |
The prefix $r$ in front of $\rdistinct{}{}$ is used mainly to
|
|
202 |
differentiate with $\textit{distinct}$, which is a built-in predicate
|
|
203 |
in Isabelle that says all the elements of a list are unique.
|
543
|
204 |
With $\textit{rdistinct}$ one can chain together all the other modules
|
|
205 |
of $\bsimp{\_}$ (removing the functionalities related to bit-sequences)
|
|
206 |
and get $\textit{rsimp}$ and $\rderssimp{\_}{\_}$.
|
|
207 |
We omit these functions, as they are routine. Please refer to the formalisation
|
|
208 |
(in file BasicIdentities.thy) for the exact definition.
|
|
209 |
With $\rrexp$ the size caclulation of annotated regular expressions'
|
|
210 |
simplification and derivatives can be done by the size of their unlifted
|
|
211 |
counterpart with the unlifted version of simplification and derivatives applied.
|
532
|
212 |
\begin{lemma}
|
553
|
213 |
The following equalities hold:
|
543
|
214 |
\begin{itemize}
|
|
215 |
\item
|
554
|
216 |
$\asize{\bsimp{a}} = \rsize{\rsimp{\rerase{a}}}$
|
|
217 |
\item
|
|
218 |
$\asize{\bderssimp{r}{s}} = \rsize{\rderssimp{\rerase{r}}{s}}$
|
|
219 |
\end{itemize}
|
532
|
220 |
\end{lemma}
|
543
|
221 |
\noindent
|
|
222 |
In the following content, we will focus on $\rrexp$'s size bound.
|
|
223 |
We will piece together this bound and show the same bound for annotated regular
|
|
224 |
expressions in the end.
|
532
|
225 |
Unless stated otherwise in this chapter all $\textit{rexp}$s without
|
|
226 |
bitcodes are seen as $\rrexp$s.
|
|
227 |
We also use $r_1 + r_2$ and $\RALTS{[r_1, r_2]}$ interchageably
|
|
228 |
as the former suits people's intuitive way of stating a binary alternative
|
|
229 |
regular expression.
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
%-----------------------------------
|
|
234 |
% SECTION ?
|
|
235 |
%-----------------------------------
|
553
|
236 |
\subsection{Finiteness Proof Using $\rrexp$s}
|
543
|
237 |
Now that we have defined the $\rrexp$ datatype, and proven that its size changes
|
|
238 |
w.r.t derivatives and simplifications mirrors precisely those of annotated regular expressions,
|
|
239 |
we aim to bound the size of $r \backslash s$ for any $\rrexp$ $r$.
|
553
|
240 |
Once we have a bound like:
|
|
241 |
\[
|
|
242 |
\llbracket r \backslash_{rsimp} s \rrbracket_r \leq N_r
|
|
243 |
\]
|
|
244 |
\noindent
|
|
245 |
we could easily extend that to
|
|
246 |
\[
|
|
247 |
\llbracket a \backslash_{bsimps} s \rrbracket \leq N_r.
|
|
248 |
\]
|
|
249 |
|
|
250 |
\subsection{Roadmap to a Bound for $\textit{Rrexp}$}
|
|
251 |
|
|
252 |
The way we obtain the bound for $\rrexp$s is by two steps:
|
543
|
253 |
\begin{itemize}
|
|
254 |
\item
|
|
255 |
First, we rewrite $r\backslash s$ into something else that is easier
|
|
256 |
to bound. This step is especially important for the inductive case
|
|
257 |
$r_1 \cdot r_2$ and $r^*$, where the derivative can grow and bloat in a wild way,
|
|
258 |
but after simplification they will always be equal or smaller to a form consisting of an alternative
|
|
259 |
list of regular expressions $f \; (g\; (\sum rs))$ with some functions applied to it, where each element will be distinct after the function application.
|
|
260 |
\item
|
|
261 |
Then, for such a sum list of regular expressions $f\; (g\; (\sum rs))$, we can control its size
|
|
262 |
by estimation, since $\distinctBy$ and $\flts$ are well-behaved and working together would only
|
553
|
263 |
reduce the size of a regular expression, not adding to it.
|
543
|
264 |
\end{itemize}
|
|
265 |
|
553
|
266 |
\section{Step One: Closed Forms}
|
|
267 |
We transform the function application $\rderssimp{r}{s}$
|
558
|
268 |
into an equivalent
|
|
269 |
form $f\; (g \; (\sum rs))$.
|
553
|
270 |
The functions $f$ and $g$ can be anything from $\flts$, $\distinctBy$ and other helper functions from $\bsimp{\_}$.
|
|
271 |
This way we get a different but equivalent way of expressing : $r\backslash s = f \; (g\; (\sum rs))$, we call the
|
|
272 |
right hand side the "closed form" of $r\backslash s$.
|
558
|
273 |
|
|
274 |
\begin{quote}\it
|
|
275 |
Claim: For regular expressions $r_1 \cdot r_2$, we claim that
|
|
276 |
\begin{center}
|
|
277 |
$ \rderssimp{r_1 \cdot r_2}{s} =
|
|
278 |
\rsimp{(\sum (r_1 \backslash s \cdot r_2 ) \; :: \;(\map \; \rderssimp{r2}{\_} \;(\vsuf{s}{r_1})))}$
|
|
279 |
\end{center}
|
|
280 |
\end{quote}
|
|
281 |
\noindent
|
|
282 |
We explain in detail how we reached those claims.
|
543
|
283 |
\subsection{Basic Properties needed for Closed Forms}
|
|
284 |
|
|
285 |
\subsubsection{$\textit{rdistinct}$'s Deduplicates Successfully}
|
|
286 |
The $\textit{rdistinct}$ function, as its name suggests, will
|
553
|
287 |
remove duplicates in an \emph{r}$\textit{rexp}$ list,
|
|
288 |
according to the accumulator
|
543
|
289 |
and leave only one of each different element in a list:
|
555
|
290 |
\begin{lemma}\label{rdistinctDoesTheJob}
|
543
|
291 |
The function $\textit{rdistinct}$ satisfies the following
|
|
292 |
properties:
|
|
293 |
\begin{itemize}
|
|
294 |
\item
|
|
295 |
If $a \in acc$ then $a \notin (\rdistinct{rs}{acc})$.
|
|
296 |
\item
|
|
297 |
If list $rs'$ is the result of $\rdistinct{rs}{acc}$,
|
555
|
298 |
then $\textit{isDistinct} \; rs'$.
|
|
299 |
\item
|
|
300 |
$\rdistinct{rs}{acc} = rs - acc$
|
543
|
301 |
\end{itemize}
|
|
302 |
\end{lemma}
|
555
|
303 |
\noindent
|
|
304 |
The predicate $\textit{isDistinct}$ is for testing
|
|
305 |
whether a list's elements are all unique. It is defined
|
|
306 |
recursively on the structure of a regular expression,
|
|
307 |
and we omit the precise definition here.
|
543
|
308 |
\begin{proof}
|
|
309 |
The first part is by an induction on $rs$.
|
555
|
310 |
The second and third part can be proven by using the
|
543
|
311 |
induction rules of $\rdistinct{\_}{\_}$.
|
|
312 |
|
|
313 |
\end{proof}
|
|
314 |
|
|
315 |
\noindent
|
|
316 |
$\rdistinct{\_}{\_}$ will cancel out all regular expression terms
|
|
317 |
that are in the accumulator, therefore prepending a list $rs_a$ with an arbitrary
|
|
318 |
list $rs$ whose elements are all from the accumulator, and then call $\rdistinct{\_}{\_}$
|
|
319 |
on the resulting list, the output will be as if we had called $\rdistinct{\_}{\_}$
|
|
320 |
without the prepending of $rs$:
|
|
321 |
\begin{lemma}
|
554
|
322 |
The elements appearing in the accumulator will always be removed.
|
|
323 |
More precisely,
|
|
324 |
\begin{itemize}
|
|
325 |
\item
|
|
326 |
If $rs \subseteq rset$, then
|
|
327 |
$\rdistinct{rs@rsa }{acc} = \rdistinct{rsa }{acc}$.
|
|
328 |
\item
|
|
329 |
Furthermore, if $a \in rset$ and $\rdistinct{rs}{\{a\}} = []$,
|
|
330 |
then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{rs'}{rset}$
|
|
331 |
\end{itemize}
|
543
|
332 |
\end{lemma}
|
554
|
333 |
|
543
|
334 |
\begin{proof}
|
|
335 |
By induction on $rs$.
|
|
336 |
\end{proof}
|
|
337 |
\noindent
|
|
338 |
On the other hand, if an element $r$ does not appear in the input list waiting to be deduplicated,
|
|
339 |
then expanding the accumulator to include that element will not cause the output list to change:
|
|
340 |
\begin{lemma}
|
|
341 |
The accumulator can be augmented to include elements not appearing in the input list,
|
|
342 |
and the output will not change.
|
|
343 |
\begin{itemize}
|
|
344 |
\item
|
|
345 |
If $r \notin rs$, then $\rdistinct{rs}{acc} = \rdistinct{rs}{\{r\} \cup acc}$.
|
|
346 |
\item
|
|
347 |
Particularly, when $acc = \varnothing$ and $rs$ de-duplicated, we have\\
|
|
348 |
\[ \rdistinct{rs}{\varnothing} = rs \]
|
|
349 |
\end{itemize}
|
|
350 |
\end{lemma}
|
|
351 |
\begin{proof}
|
|
352 |
The first half is by induction on $rs$. The second half is a corollary of the first.
|
|
353 |
\end{proof}
|
|
354 |
\noindent
|
|
355 |
The next property gives the condition for
|
|
356 |
when $\rdistinct{\_}{\_}$ becomes an identical mapping
|
|
357 |
for any prefix of an input list, in other words, when can
|
|
358 |
we ``push out" the arguments of $\rdistinct{\_}{\_}$:
|
555
|
359 |
\begin{lemma}\label{distinctRdistinctAppend}
|
554
|
360 |
If $\textit{isDistinct} \; rs_1$, and $rs_1 \cap acc = \varnothing$,
|
555
|
361 |
then
|
553
|
362 |
\[\textit{rdistinct}\; (rs_1 @ rsa)\;\, acc
|
|
363 |
= rs_1@(\textit{rdistinct} rsa \; (acc \cup rs_1))\]
|
543
|
364 |
\end{lemma}
|
554
|
365 |
\noindent
|
555
|
366 |
In other words, it can be taken out and left untouched in the output.
|
543
|
367 |
\begin{proof}
|
|
368 |
By an induction on $rs_1$, where $rsa$ and $acc$ are allowed to be arbitrary.
|
|
369 |
\end{proof}
|
554
|
370 |
\noindent
|
|
371 |
$\rdistinct{}{}$ removes any element in anywhere of a list, if it
|
|
372 |
had appeared previously:
|
|
373 |
\begin{lemma}\label{distinctRemovesMiddle}
|
|
374 |
The two properties hold if $r \in rs$:
|
|
375 |
\begin{itemize}
|
|
376 |
\item
|
555
|
377 |
$\rdistinct{rs}{rset} = \rdistinct{(rs @ [r])}{rset}$\\
|
|
378 |
and\\
|
554
|
379 |
$\rdistinct{(ab :: rs @ [ab])}{rset'} = \rdistinct{(ab :: rs)}{rset'}$
|
|
380 |
\item
|
555
|
381 |
$\rdistinct{ (rs @ rs') }{rset} = \rdistinct{rs @ [r] @ rs'}{rset}$\\
|
|
382 |
and\\
|
554
|
383 |
$\rdistinct{(ab :: rs @ [ab] @ rs'')}{rset'} =
|
|
384 |
\rdistinct{(ab :: rs @ rs'')}{rset'}$
|
|
385 |
\end{itemize}
|
|
386 |
\end{lemma}
|
|
387 |
\noindent
|
|
388 |
\begin{proof}
|
|
389 |
By induction on $rs$. All other variables are allowed to be arbitrary.
|
|
390 |
The second half of the lemma requires the first half.
|
|
391 |
Note that for each half's two sub-propositions need to be proven concurrently,
|
|
392 |
so that the induction goes through.
|
|
393 |
\end{proof}
|
|
394 |
|
555
|
395 |
\noindent
|
|
396 |
This allows us to prove ``Idempotency" of $\rdistinct{}{}$ of some kind:
|
|
397 |
\begin{lemma}\label{rdistinctConcatGeneral}
|
|
398 |
The following equalities involving multiple applications of $\rdistinct{}{}$ hold:
|
|
399 |
\begin{itemize}
|
|
400 |
\item
|
|
401 |
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{((\rdistinct{rs}{\varnothing})@ rs')}{\varnothing}$
|
|
402 |
\item
|
|
403 |
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{(\rdistinct{rs}{\varnothing} @ rs')}{\varnothing}$
|
|
404 |
\item
|
|
405 |
If $rset' \subseteq rset$, then $\rdistinct{rs}{rset} =
|
|
406 |
\rdistinct{(\rdistinct{rs}{rset'})}{rset}$. As a corollary
|
|
407 |
of this,
|
|
408 |
\item
|
|
409 |
$\rdistinct{(rs @ rs')}{rset} = \rdistinct{
|
|
410 |
(\rdistinct{rs}{\varnothing}) @ rs')}{rset}$. This
|
|
411 |
gives another corollary use later:
|
|
412 |
\item
|
|
413 |
If $a \in rset$, then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{
|
|
414 |
(\rdistinct{(a :: rs)}{\varnothing} @ rs')}{rset} $,
|
|
415 |
|
|
416 |
\end{itemize}
|
|
417 |
\end{lemma}
|
|
418 |
\begin{proof}
|
|
419 |
By \ref{rdistinctDoesTheJob} and \ref{distinctRemovesMiddle}.
|
|
420 |
\end{proof}
|
|
421 |
|
553
|
422 |
\subsubsection{The Properties of $\backslash_r$, $\backslash_{rsimp}$, $\textit{Rflts}$ and $\textit{Rsimp}_{ALTS}$}
|
|
423 |
We give in this subsection some properties of how $\backslash_r$, $\backslash_{rsimp}$, $\textit{Rflts}$ and $\textit{Rsimp}_{ALTS} $ interact with each other and with $@$, the concatenation operator.
|
543
|
424 |
These will be helpful in later closed form proofs, when
|
|
425 |
we want to transform the ways in which multiple functions involving
|
|
426 |
those are composed together
|
|
427 |
in interleaving derivative and simplification steps.
|
|
428 |
|
|
429 |
When the function $\textit{Rflts}$
|
|
430 |
is applied to the concatenation of two lists, the output can be calculated by first applying the
|
|
431 |
functions on two lists separately, and then concatenating them together.
|
554
|
432 |
\begin{lemma}\label{rfltsProps}
|
543
|
433 |
The function $\rflts$ has the below properties:\\
|
|
434 |
\begin{itemize}
|
|
435 |
\item
|
554
|
436 |
$\rflts \; (rs_1 @ rs_2) = \rflts \; rs_1 @ \rflts \; rs_2$
|
|
437 |
\item
|
|
438 |
If $r \neq \RZERO$ and $\nexists rs_1. r = \RALTS{rs}_1$, then $\rflts \; (r::rs) = r :: \rflts \; rs$
|
|
439 |
\item
|
|
440 |
$\rflts \; (rs @ [\RZERO]) = \rflts \; rs$
|
|
441 |
\item
|
|
442 |
$\rflts \; (rs' @ [\RALTS{rs}]) = \rflts \; rs'@rs$
|
|
443 |
\item
|
|
444 |
$\rflts \; (rs @ [\RONE]) = \rflts \; rs @ [\RONE]$
|
|
445 |
\item
|
|
446 |
If $r \neq \RZERO$ and $\nexists rs'. r = \RALTS{rs'}$ then $\rflts \; (rs @ [r])
|
|
447 |
= (\rflts \; rs) @ [r]$
|
555
|
448 |
\item
|
|
449 |
If $r = \RALTS{rs}$ and $r \in rs'$ then for all $r_1 \in rs.
|
|
450 |
r_1 \in \rflts \; rs'$.
|
|
451 |
\item
|
|
452 |
$\rflts \; (rs_a @ \RZERO :: rs_b) = \rflts \; (rs_a @ rs_b)$
|
543
|
453 |
\end{itemize}
|
|
454 |
\end{lemma}
|
|
455 |
\noindent
|
|
456 |
\begin{proof}
|
555
|
457 |
By induction on $rs_1$ in the first sub-lemma, and induction on $r$ in the second part,
|
|
458 |
and induction on $rs$, $rs'$, $rs$, $rs'$, $rs_a$ in the third, fourth, fifth, sixth and
|
|
459 |
last sub-lemma.
|
543
|
460 |
\end{proof}
|
555
|
461 |
|
554
|
462 |
\subsubsection{The $RL$ Function: Language Interpretation of $\textit{Rrexp}$s}
|
|
463 |
Much like the definition of $L$ on plain regular expressions, one could also
|
|
464 |
define the language interpretation of $\rrexp$s.
|
|
465 |
\begin{center}
|
|
466 |
\begin{tabular}{lcl}
|
|
467 |
$RL \; (\ZERO)$ & $\dn$ & $\phi$\\
|
|
468 |
$RL \; (\ONE)$ & $\dn$ & $\{[]\}$\\
|
|
469 |
$RL \; (c)$ & $\dn$ & $\{[c]\}$\\
|
|
470 |
$RL \; \sum rs$ & $\dn$ & $ \bigcup_{r \in rs} (RL \; r)$\\
|
|
471 |
$RL \; (r_1 \cdot r_2)$ & $\dn$ & $ RL \; (r_1) @ RL \; (r_2)$\\
|
|
472 |
$RL \; (r^*)$ & $\dn$ & $ (RL(r))^*$
|
|
473 |
\end{tabular}
|
|
474 |
\end{center}
|
|
475 |
\noindent
|
|
476 |
The main use of $RL$ is to establish some connections between $\rsimp{}$
|
|
477 |
and $\rnullable{}$:
|
|
478 |
\begin{lemma}
|
|
479 |
The following properties hold:
|
|
480 |
\begin{itemize}
|
|
481 |
\item
|
|
482 |
If $\rnullable{r}$, then $\rsimp{r} \neq \RZERO$.
|
|
483 |
\item
|
|
484 |
$\rnullable{r \backslash s} \quad $ if and only if $\quad \rnullable{\rderssimp{r}{s}}$.
|
|
485 |
\end{itemize}
|
|
486 |
\end{lemma}
|
|
487 |
\begin{proof}
|
|
488 |
The first part is by induction on $r$.
|
|
489 |
The second part is true because property
|
|
490 |
\[ RL \; r = RL \; (\rsimp{r})\] holds.
|
|
491 |
\end{proof}
|
|
492 |
|
|
493 |
\subsubsection{$\rsimp{}$ is Non-Increasing}
|
|
494 |
In this subsection, we prove that the function $\rsimp{}$ does not
|
|
495 |
make the $\llbracket \rrbracket_r$ size increase.
|
543
|
496 |
|
|
497 |
|
554
|
498 |
\begin{lemma}\label{rsimpSize}
|
|
499 |
$\llbracket \rsimp{r} \rrbracket_r \leq \llbracket r \rrbracket_r$
|
|
500 |
\end{lemma}
|
|
501 |
\subsubsection{Simplified $\textit{Rrexp}$s are Good}
|
|
502 |
We formalise the notion of ``good" regular expressions,
|
|
503 |
which means regular expressions that
|
|
504 |
are not fully simplified. For alternative regular expressions that means they
|
|
505 |
do not contain any nested alternatives like
|
|
506 |
\[ r_1 + (r_2 + r_3) \], un-removed $\RZERO$s like \[\RZERO + r\]
|
|
507 |
or duplicate elements in a children regular expression list like \[ \sum [r, r, \ldots]\]:
|
|
508 |
\begin{center}
|
|
509 |
\begin{tabular}{@{}lcl@{}}
|
|
510 |
$\good\; \RZERO$ & $\dn$ & $\textit{false}$\\
|
|
511 |
$\good\; \RONE$ & $\dn$ & $\textit{true}$\\
|
|
512 |
$\good\; \RCHAR{c}$ & $\dn$ & $\btrue$\\
|
|
513 |
$\good\; \RALTS{[]}$ & $\dn$ & $\bfalse$\\
|
|
514 |
$\good\; \RALTS{[r]}$ & $\dn$ & $\bfalse$\\
|
|
515 |
$\good\; \RALTS{r_1 :: r_2 :: rs}$ & $\dn$ &
|
|
516 |
$\textit{isDistinct} \; (r_1 :: r_2 :: rs) \;$\\
|
|
517 |
& & $\textit{and}\; (\forall r' \in (r_1 :: r_2 :: rs).\; \good \; r'\; \, \textit{and}\; \, \textit{nonAlt}\; r')$\\
|
|
518 |
$\good \; \RSEQ{\RZERO}{r}$ & $\dn$ & $\bfalse$\\
|
|
519 |
$\good \; \RSEQ{\RONE}{r}$ & $\dn$ & $\bfalse$\\
|
|
520 |
$\good \; \RSEQ{r}{\RZERO}$ & $\dn$ & $\bfalse$\\
|
|
521 |
$\good \; \RSEQ{r_1}{r_2}$ & $\dn$ & $\good \; r_1 \;\, \textit{and} \;\, \good \; r_2$\\
|
|
522 |
$\good \; \RSTAR{r}$ & $\dn$ & $\btrue$\\
|
|
523 |
\end{tabular}
|
|
524 |
\end{center}
|
|
525 |
\noindent
|
|
526 |
The predicate $\textit{nonAlt}$ evaluates to true when the regular expression is not an
|
|
527 |
alternative, and false otherwise.
|
|
528 |
The $\good$ property is preserved under $\rsimp_{ALTS}$, provided that
|
|
529 |
its non-empty argument list of expressions are all good themsleves, and $\textit{nonAlt}$,
|
|
530 |
and unique:
|
|
531 |
\begin{lemma}\label{rsimpaltsGood}
|
|
532 |
If $rs \neq []$ and forall $r \in rs. \textit{nonAlt} \; r$ and $\textit{isDistinct} \; rs$,
|
|
533 |
then $\good \; (\rsimpalts \; rs)$ if and only if forall $r \in rs. \; \good \; r$.
|
|
534 |
\end{lemma}
|
|
535 |
\noindent
|
|
536 |
We also note that
|
|
537 |
if a regular expression $r$ is good, then $\rflts$ on the singleton
|
|
538 |
list $[r]$ will not break goodness:
|
|
539 |
\begin{lemma}\label{flts2}
|
|
540 |
If $\good \; r$, then forall $r' \in \rflts \; [r]. \; \good \; r'$ and $\textit{nonAlt} \; r'$.
|
|
541 |
\end{lemma}
|
|
542 |
\begin{proof}
|
|
543 |
By an induction on $r$.
|
|
544 |
\end{proof}
|
543
|
545 |
\noindent
|
554
|
546 |
The other observation we make about $\rsimp{r}$ is that it never
|
|
547 |
comes with nested alternatives, which we describe as the $\nonnested$
|
|
548 |
property:
|
|
549 |
\begin{center}
|
|
550 |
\begin{tabular}{lcl}
|
|
551 |
$\nonnested \; \, \sum []$ & $\dn$ & $\btrue$\\
|
|
552 |
$\nonnested \; \, \sum ((\sum rs_1) :: rs_2)$ & $\dn$ & $\bfalse$\\
|
|
553 |
$\nonnested \; \, \sum (r :: rs)$ & $\dn$ & $\nonnested (\sum rs)$\\
|
|
554 |
$\nonnested \; \, r $ & $\dn$ & $\btrue$
|
|
555 |
\end{tabular}
|
|
556 |
\end{center}
|
|
557 |
\noindent
|
|
558 |
The $\rflts$ function
|
|
559 |
always opens up nested alternatives,
|
|
560 |
which enables $\rsimp$ to be non-nested:
|
|
561 |
|
|
562 |
\begin{lemma}\label{nonnestedRsimp}
|
|
563 |
$\nonnested \; (\rsimp{r})$
|
|
564 |
\end{lemma}
|
|
565 |
\begin{proof}
|
|
566 |
By an induction on $r$.
|
|
567 |
\end{proof}
|
|
568 |
\noindent
|
|
569 |
With this we could prove that a regular expressions
|
|
570 |
after simplification and flattening and de-duplication,
|
|
571 |
will not contain any alternative regular expression directly:
|
|
572 |
\begin{lemma}\label{nonaltFltsRd}
|
|
573 |
If $x \in \rdistinct{\rflts\; (\map \; \rsimp{} \; rs)}{\varnothing}$
|
|
574 |
then $\textit{nonAlt} \; x$.
|
|
575 |
\end{lemma}
|
|
576 |
\begin{proof}
|
|
577 |
By \ref{nonnestedRsimp}.
|
|
578 |
\end{proof}
|
|
579 |
\noindent
|
|
580 |
The other thing we know is that once $\rsimp{}$ had finished
|
|
581 |
processing an alternative regular expression, it will not
|
|
582 |
contain any $\RZERO$s, this is because all the recursive
|
|
583 |
calls to the simplification on the children regular expressions
|
|
584 |
make the children good, and $\rflts$ will not take out
|
|
585 |
any $\RZERO$s out of a good regular expression list,
|
|
586 |
and $\rdistinct{}$ will not mess with the result.
|
|
587 |
\begin{lemma}\label{flts3Obv}
|
|
588 |
The following are true:
|
|
589 |
\begin{itemize}
|
|
590 |
\item
|
|
591 |
If for all $r \in rs. \, \good \; r $ or $r = \RZERO$,
|
|
592 |
then for all $r \in \rflts\; rs. \, \good \; r$.
|
|
593 |
\item
|
|
594 |
If $x \in \rdistinct{\rflts\; (\map \; rsimp{}\; rs)}{\varnothing}$
|
|
595 |
and for all $y$ such that $\llbracket y \rrbracket_r$ less than
|
|
596 |
$\llbracket rs \rrbracket_r + 1$, either
|
|
597 |
$\good \; (\rsimp{y})$ or $\rsimp{y} = \RZERO$,
|
|
598 |
then $\good \; x$.
|
|
599 |
\end{itemize}
|
|
600 |
\end{lemma}
|
|
601 |
\begin{proof}
|
|
602 |
The first part is by induction on $rs$, where the induction
|
|
603 |
rule is the inductive cases for $\rflts$.
|
|
604 |
The second part is a corollary from the first part.
|
|
605 |
\end{proof}
|
543
|
606 |
|
554
|
607 |
And this leads to good structural property of $\rsimp{}$,
|
|
608 |
that after simplification, a regular expression is
|
|
609 |
either good or $\RZERO$:
|
|
610 |
\begin{lemma}\label{good1}
|
|
611 |
For any r-regular expression $r$, $\good \; \rsimp{r}$ or $\rsimp{r} = \RZERO$.
|
|
612 |
\end{lemma}
|
|
613 |
\begin{proof}
|
|
614 |
By an induction on $r$. The inductive measure is the size $\llbracket \rrbracket_r$.
|
|
615 |
Lemma \ref{rsimpSize} says that
|
|
616 |
$\llbracket \rsimp{r}\rrbracket_r$ is smaller than or equal to
|
|
617 |
$\llbracket r \rrbracket_r$.
|
|
618 |
Therefore, in the $r_1 \cdot r_2$ and $\sum rs$ case,
|
|
619 |
Inductive hypothesis applies to the children regular expressions
|
|
620 |
$r_1$, $r_2$, etc. The lemma \ref{flts3Obv}'s precondition is satisfied
|
|
621 |
by that as well.
|
|
622 |
The lemmas \ref{nonnestedRsimp} and \ref{nonaltFltsRd} are used
|
|
623 |
to ensure that goodness is preserved at the topmost level.
|
|
624 |
\end{proof}
|
|
625 |
We shall prove that any good regular expression is
|
|
626 |
a fixed-point for $\rsimp{}$.
|
|
627 |
First we prove an auxiliary lemma:
|
|
628 |
\begin{lemma}\label{goodaltsNonalt}
|
|
629 |
If $\good \; \sum rs$, then $\rflts\; rs = rs$.
|
|
630 |
\end{lemma}
|
|
631 |
\begin{proof}
|
|
632 |
By an induction on $\sum rs$. The inductive rules are the cases
|
|
633 |
for $\good$.
|
|
634 |
\end{proof}
|
|
635 |
\noindent
|
|
636 |
Now we are ready to prove that good regular expressions are invariant
|
|
637 |
of $\rsimp{}$ application:
|
|
638 |
\begin{lemma}\label{test}
|
|
639 |
If $\good \;r$ then $\rsimp{r} = r$.
|
|
640 |
\end{lemma}
|
|
641 |
\begin{proof}
|
|
642 |
By an induction on the inductive cases of $\good$.
|
|
643 |
The lemma \ref{goodaltsNonalt} is used in the alternative
|
|
644 |
case where 2 or more elements are present in the list.
|
|
645 |
\end{proof}
|
555
|
646 |
\noindent
|
|
647 |
Given below is a property involving $\rflts$, $\rdistinct{}{}$, $\rsimp{}$ and $\rsimp_{ALTS}$,
|
|
648 |
which requires $\ref{good1}$ to go through smoothly.
|
|
649 |
It says that an application of $\rsimp_{ALTS}$ can be "absorbed",
|
|
650 |
if it its output is concatenated with a list and then applied to $\rflts$.
|
|
651 |
\begin{lemma}\label{flattenRsimpalts}
|
|
652 |
$\rflts \; ( (\rsimp_{ALTS} \;
|
|
653 |
(\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing})) ::
|
|
654 |
\map \; \rsimp{} \; rs' ) =
|
|
655 |
\rflts \; ( (\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing}) @ (
|
|
656 |
\map \; \rsimp{rs'}))$
|
554
|
657 |
|
555
|
658 |
|
|
659 |
\end{lemma}
|
|
660 |
\begin{proof}
|
|
661 |
By \ref{good1}.
|
|
662 |
\end{proof}
|
|
663 |
\noindent
|
|
664 |
|
|
665 |
|
|
666 |
|
|
667 |
|
|
668 |
|
|
669 |
We are also
|
554
|
670 |
\subsubsection{$\rsimp$ is Idempotent}
|
|
671 |
The idempotency of $\rsimp$ is very useful in
|
|
672 |
manipulating regular expression terms into desired
|
|
673 |
forms so that key steps allowing further rewriting to closed forms
|
|
674 |
are possible.
|
|
675 |
\begin{lemma}\label{rsimpIdem}
|
|
676 |
$\rsimp{r} = \rsimp{\rsimp{r}}$
|
|
677 |
\end{lemma}
|
|
678 |
|
|
679 |
\begin{proof}
|
|
680 |
By \ref{test} and \ref{good1}.
|
|
681 |
\end{proof}
|
|
682 |
\noindent
|
|
683 |
This property means we do not have to repeatedly
|
|
684 |
apply simplification in each step, which justifies
|
|
685 |
our definition of $\blexersimp$.
|
|
686 |
|
532
|
687 |
|
554
|
688 |
On the other hand, we could repeat the same $\rsimp{}$ applications
|
|
689 |
on regular expressions as many times as we want, if we have at least
|
|
690 |
one simplification applied to it, and apply it wherever we would like to:
|
|
691 |
\begin{corollary}\label{headOneMoreSimp}
|
555
|
692 |
The following properties hold, directly from \ref{rsimpIdem}:
|
|
693 |
|
|
694 |
\begin{itemize}
|
|
695 |
\item
|
|
696 |
$\map \; \rsimp{(r :: rs)} = \map \; \rsimp{} \; (\rsimp{r} :: rs)$
|
|
697 |
\item
|
|
698 |
$\rsimp{(\RALTS{rs})} = \rsimp{(\RALTS{\map \; \rsimp{} \; rs})}$
|
|
699 |
\end{itemize}
|
554
|
700 |
\end{corollary}
|
|
701 |
\noindent
|
|
702 |
This will be useful in later closed form proof's rewriting steps.
|
|
703 |
Similarly, we point out the following useful facts below:
|
|
704 |
\begin{lemma}
|
|
705 |
The following equalities hold if $r = \rsimp{r'}$ for some $r'$:
|
|
706 |
\begin{itemize}
|
|
707 |
\item
|
|
708 |
If $r = \sum rs$ then $\rsimpalts \; rs = \sum rs$.
|
|
709 |
\item
|
|
710 |
If $r = \sum rs$ then $\rdistinct{rs}{\varnothing} = rs$.
|
|
711 |
\item
|
|
712 |
$\rsimpalts \; (\rdistinct{\rflts \; [r]}{\varnothing}) = r$.
|
|
713 |
\end{itemize}
|
|
714 |
\end{lemma}
|
|
715 |
\begin{proof}
|
|
716 |
By application of \ref{rsimpIdem} and \ref{good1}.
|
|
717 |
\end{proof}
|
|
718 |
|
|
719 |
\noindent
|
|
720 |
With the idempotency of $\rsimp{}$ and its corollaries,
|
|
721 |
we can start proving some key equalities leading to the
|
|
722 |
closed forms.
|
|
723 |
Now presented are a few equivalent terms under $\rsimp{}$.
|
|
724 |
We use $r_1 \sequal r_2 $ here to denote $\rsimp{r_1} = \rsimp{r_2}$.
|
|
725 |
\begin{lemma}
|
|
726 |
\begin{itemize}
|
555
|
727 |
The following equivalence hold:
|
554
|
728 |
\item
|
|
729 |
$\rsimpalts \; (\RZERO :: rs) \sequal \rsimpalts\; rs$
|
|
730 |
\item
|
|
731 |
$\rsimpalts \; rs \sequal \rsimpalts (\map \; \rsimp{} \; rs)$
|
|
732 |
\item
|
|
733 |
$\RALTS{\RALTS{rs}} \sequal \RALTS{rs}$
|
555
|
734 |
\item
|
|
735 |
$\sum ((\sum rs_a) :: rs_b) \sequal \sum rs_a @ rs_b$
|
|
736 |
\item
|
|
737 |
$\RALTS{rs} = \RALTS{\map \; \rsimp{} \; rs}$
|
554
|
738 |
\end{itemize}
|
|
739 |
\end{lemma}
|
555
|
740 |
\begin{proof}
|
|
741 |
By induction on the lists involved.
|
|
742 |
\end{proof}
|
|
743 |
\noindent
|
|
744 |
Similarly,
|
|
745 |
we introduce the equality for $\sum$ when certain child regular expressions
|
|
746 |
are $\sum$ themselves:
|
|
747 |
\begin{lemma}\label{simpFlatten3}
|
|
748 |
One can flatten the inside $\sum$ of a $\sum$ if it is being
|
|
749 |
simplified. Concretely,
|
|
750 |
\begin{itemize}
|
|
751 |
\item
|
|
752 |
If for all $r \in rs, rs', rs''$, we have $\good \; r $
|
|
753 |
or $r = \RZERO$, then $\sum (rs' @ rs @ rs'') \sequal
|
|
754 |
\sum (rs' @ [\sum rs] @ rs'')$ holds. As a corollary,
|
|
755 |
\item
|
|
756 |
$\sum (rs' @ [\sum rs] @ rs'') \sequal \sum (rs' @ rs @ rs'')$
|
|
757 |
\end{itemize}
|
|
758 |
\end{lemma}
|
|
759 |
\begin{proof}
|
|
760 |
By rewriting steps involving the use of \ref{test} and \ref{rdistinctConcatGeneral}.
|
|
761 |
The second sub-lemma is a corollary of the previous.
|
|
762 |
\end{proof}
|
|
763 |
%Rewriting steps not put in--too long and complicated-------------------------------
|
|
764 |
\begin{comment}
|
|
765 |
\begin{center}
|
|
766 |
$\rsimp{\sum (rs' @ rs @ rs'')} \stackrel{def of bsimp}{=}$ \\
|
|
767 |
$\rsimpalts \; (\rdistinct{\rflts \; ((\map \; \rsimp{}\; rs') @ (\map \; \rsimp{} \; rs ) @ (\map \; \rsimp{} \; rs''))}{\varnothing})$ \\
|
|
768 |
$\stackrel{by \ref{test}}{=}
|
|
769 |
\rsimpalts \; (\rdistinct{(\rflts \; rs' @ \rflts \; rs @ \rflts \; rs'')}{
|
|
770 |
\varnothing})$\\
|
|
771 |
$\stackrel{by \ref{rdistinctConcatGeneral}}{=}
|
|
772 |
\rsimpalts \; (\rdistinct{\rflts \; rs'}{\varnothing} @ \rdistinct{(
|
|
773 |
\rflts\; rs @ \rflts \; rs'')}{\rflts \; rs'})$\\
|
|
774 |
|
|
775 |
\end{center}
|
|
776 |
\end{comment}
|
|
777 |
%Rewriting steps not put in--too long and complicated-------------------------------
|
554
|
778 |
\noindent
|
|
779 |
We need more equalities like the above to enable a closed form,
|
|
780 |
but to proceed we need to introduce two rewrite relations,
|
|
781 |
to make things smoother.
|
557
|
782 |
\subsubsection{The rewrite relation $\hrewrite$ , $\scfrewrites$ , $\frewrite$ and $\grewrite$}
|
554
|
783 |
Insired by the success we had in the correctness proof
|
|
784 |
in \ref{Bitcoded2}, where we invented
|
555
|
785 |
a term rewriting system to capture the similarity between terms,
|
|
786 |
we follow suit here defining simplification
|
|
787 |
steps as rewriting steps. This allows capturing
|
|
788 |
similarities between terms that would be otherwise
|
|
789 |
hard to express.
|
|
790 |
|
557
|
791 |
We use $\hrewrite$ for one-step atomic rewrite of
|
|
792 |
regular expression simplification,
|
555
|
793 |
$\frewrite$ for rewrite of list of regular expressions that
|
|
794 |
include all operations carried out in $\rflts$, and $\grewrite$ for
|
|
795 |
rewriting a list of regular expressions possible in both $\rflts$ and $\rdistinct{}{}$.
|
|
796 |
Their reflexive transitive closures are used to denote zero or many steps,
|
|
797 |
as was the case in the previous chapter.
|
554
|
798 |
The presentation will be more concise than that in \ref{Bitcoded2}.
|
|
799 |
To differentiate between the rewriting steps for annotated regular expressions
|
|
800 |
and $\rrexp$s, we add characters $h$ and $g$ below the squig arrow symbol
|
|
801 |
to mean atomic simplification transitions
|
|
802 |
of $\rrexp$s and $\rrexp$ lists, respectively.
|
|
803 |
|
555
|
804 |
|
|
805 |
|
|
806 |
List of one-step rewrite rules for $\rrexp$ ($\hrewrite$):
|
|
807 |
|
|
808 |
|
554
|
809 |
\begin{center}
|
555
|
810 |
\begin{mathpar}
|
|
811 |
\inferrule[RSEQ0L]{}{\RZERO \cdot r_2 \hrewrite \RZERO\\}
|
|
812 |
|
|
813 |
\inferrule[RSEQ0R]{}{r_1 \cdot \RZERO \hrewrite \RZERO\\}
|
|
814 |
|
|
815 |
\inferrule[RSEQ1]{}{(\RONE \cdot r) \hrewrite r\\}\\
|
|
816 |
|
|
817 |
\inferrule[RSEQL]{ r_1 \hrewrite r_2}{r_1 \cdot r_3 \hrewrite r_2 \cdot r_3\\}
|
|
818 |
|
|
819 |
\inferrule[RSEQR]{ r_3 \hrewrite r_4}{r_1 \cdot r_3 \hrewrite r_1 \cdot r_4\\}\\
|
|
820 |
|
|
821 |
\inferrule[RALTSChild]{r \hrewrite r'}{\sum (rs_1 @ [r] @ rs_2) \hrewrite \sum (rs_1 @ [r'] @ rs_2)\\}
|
|
822 |
|
|
823 |
\inferrule[RALTS0]{}{\sum (rs_a @ [\RZERO] @ rs_b) \hrewrite \sum (rs_a @ rs_b)}
|
|
824 |
|
|
825 |
\inferrule[RALTSNested]{}{\sum (rs_a @ [\sum rs_1] @ rs_b) \hrewrite \sum (rs_a @ rs_1 @ rs_b)}
|
|
826 |
|
|
827 |
\inferrule[RALTSNil]{}{ \sum [] \hrewrite \RZERO\\}
|
|
828 |
|
|
829 |
\inferrule[RALTSSingle]{}{ \sum [r] \hrewrite r\\}
|
|
830 |
|
|
831 |
\inferrule[RALTSDelete]{\\ r_1 = r_2}{\sum rs_a @ [r_1] @ rs_b @ [r_2] @ rsc \hrewrite \sum rs_a @ [r_1] @ rs_b @ rs_c}
|
|
832 |
|
|
833 |
\end{mathpar}
|
|
834 |
\end{center}
|
554
|
835 |
|
557
|
836 |
|
|
837 |
List of rewrite rules for a list of regular expressions,
|
|
838 |
where each element can rewrite in many steps to the other (scf stands for
|
|
839 |
li\emph{s}t \emph{c}losed \emph{f}orm). This relation is similar to the
|
|
840 |
$\stackrel{s*}{\rightsquigarrow}$ for annotated regular expressions.
|
|
841 |
|
|
842 |
\begin{center}
|
|
843 |
\begin{mathpar}
|
|
844 |
\inferrule{}{[] \scfrewrites [] }
|
|
845 |
\inferrule{r \hrewrites r' \\ rs \scfrewrites rs'}{r :: rs \scfrewrites r' :: rs'}
|
|
846 |
\end{mathpar}
|
|
847 |
\end{center}
|
555
|
848 |
%frewrite
|
|
849 |
List of one-step rewrite rules for flattening
|
|
850 |
a list of regular expressions($\frewrite$):
|
|
851 |
\begin{center}
|
|
852 |
\begin{mathpar}
|
|
853 |
\inferrule{}{\RZERO :: rs \frewrite rs \\}
|
|
854 |
|
|
855 |
\inferrule{}{(\sum rs) :: rs_a \frewrite rs @ rs_a \\}
|
|
856 |
|
|
857 |
\inferrule{rs_1 \frewrite rs_2}{r :: rs_1 \frewrite r :: rs_2}
|
|
858 |
\end{mathpar}
|
|
859 |
\end{center}
|
|
860 |
|
|
861 |
Lists of one-step rewrite rules for flattening and de-duplicating
|
|
862 |
a list of regular expressions ($\grewrite$):
|
|
863 |
\begin{center}
|
|
864 |
\begin{mathpar}
|
557
|
865 |
\inferrule{}{\RZERO :: rs \grewrite rs \\}
|
532
|
866 |
|
557
|
867 |
\inferrule{}{(\sum rs) :: rs_a \grewrite rs @ rs_a \\}
|
555
|
868 |
|
557
|
869 |
\inferrule{rs_1 \grewrite rs_2}{r :: rs_1 \grewrite r :: rs_2}
|
555
|
870 |
|
|
871 |
\inferrule[dB]{}{rs_a @ [a] @ rs_b @[a] @ rs_c \grewrite rs_a @ [a] @ rsb @ rsc}
|
|
872 |
\end{mathpar}
|
|
873 |
\end{center}
|
|
874 |
|
|
875 |
\noindent
|
|
876 |
The reason why we take the trouble of defining
|
|
877 |
two separate list rewriting definitions $\frewrite$ and $\grewrite$
|
557
|
878 |
is to separate the two stages of simplification: flattening and de-duplicating.
|
|
879 |
Sometimes $\grewrites$ is slightly too powerful
|
|
880 |
so we would rather use $\frewrites$ which makes certain rewriting steps
|
|
881 |
more straightforward to prove.
|
556
|
882 |
For example, when proving the closed-form for the alternative regular expression,
|
|
883 |
one of the rewriting steps would be:
|
|
884 |
\begin{lemma}
|
557
|
885 |
$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
|
|
886 |
\sum (\rDistinct \;\; (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
|
556
|
887 |
$
|
|
888 |
\end{lemma}
|
|
889 |
\noindent
|
|
890 |
Proving this is by first showing
|
557
|
891 |
\begin{lemma}\label{earlyLaterDerFrewrites}
|
556
|
892 |
$\map \; (\_ \backslash x) \; (\rflts \; rs) \frewrites
|
557
|
893 |
\rflts \; (\map \; (\_ \backslash x) \; rs)$
|
556
|
894 |
\end{lemma}
|
|
895 |
\noindent
|
|
896 |
and then using lemma
|
|
897 |
\begin{lemma}\label{frewritesSimpeq}
|
|
898 |
If $rs_1 \frewrites rs_2 $, then $\sum (\rDistinct \; rs_1 \; \varnothing) \sequal
|
557
|
899 |
\sum (\rDistinct \; rs_2 \; \varnothing)$.
|
556
|
900 |
\end{lemma}
|
557
|
901 |
\noindent
|
|
902 |
is a piece of cake.
|
|
903 |
But this trick will not work for $\grewrites$.
|
|
904 |
For example, a rewriting step in proving
|
|
905 |
closed forms is:
|
|
906 |
\begin{center}
|
|
907 |
$\rsimp{(\rsimpalts \; (\map \; (\_ \backslash x) \; (\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs}))))}{\varnothing})))}$\\
|
|
908 |
$=$ \\
|
|
909 |
$\rsimp{(\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \; (\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}))} $
|
|
910 |
\noindent
|
|
911 |
\end{center}
|
|
912 |
For this one would hope to have a rewriting relation between the two lists involved,
|
|
913 |
similar to \ref{earlyLaterDerFrewrites}. However, it turns out that
|
556
|
914 |
\begin{center}
|
|
915 |
$\map \; (\_ \backslash x) \; (\rDistinct \; rs \; rset) \grewrites \rDistinct \; (\map \;
|
557
|
916 |
(\_ \backslash x) \; rs) \; ( rset \backslash x)$
|
556
|
917 |
\end{center}
|
|
918 |
\noindent
|
557
|
919 |
does $\mathbf{not}$ hold in general.
|
|
920 |
For this rewriting step we will introduce some slightly more cumbersome
|
|
921 |
proof technique in later sections.
|
|
922 |
The point is that $\frewrite$
|
|
923 |
allows us to prove equivalence in a straightforward two-step method that is
|
|
924 |
not possible for $\grewrite$, thereby reducing the complexity of the entire proof.
|
555
|
925 |
|
556
|
926 |
|
557
|
927 |
\subsubsection{Terms That Can Be Rewritten Using $\hrewrites$, $\grewrites$, and $\frewrites$}
|
|
928 |
We present in the below lemma a few pairs of terms that are rewritable via
|
|
929 |
$\grewrites$:
|
|
930 |
\begin{lemma}\label{gstarRdistinctGeneral}
|
|
931 |
\begin{itemize}
|
|
932 |
\item
|
|
933 |
$rs_1 @ rs \grewrites rs_1 @ (\rDistinct \; rs \; rs_1)$
|
|
934 |
\item
|
|
935 |
$rs \grewrites \rDistinct \; rs \; \varnothing$
|
|
936 |
\item
|
|
937 |
$rs_a @ (\rDistinct \; rs \; rs_a) \grewrites rs_a @ (\rDistinct \;
|
|
938 |
rs \; (\{\RZERO\} \cup rs_a))$
|
|
939 |
\item
|
|
940 |
$rs \;\; @ \;\; \rDistinct \; rs_a \; rset \grewrites rs @ \rDistinct \; rs_a \;
|
|
941 |
(rest \cup rs)$
|
|
942 |
|
|
943 |
\end{itemize}
|
|
944 |
\end{lemma}
|
|
945 |
\noindent
|
|
946 |
If a pair of terms $rs_1, rs_2$ are rewritable via $\grewrites$ to each other,
|
|
947 |
then they are equivalent under $\rsimp{}$:
|
|
948 |
\begin{lemma}\label{grewritesSimpalts}
|
|
949 |
If $rs_1 \grewrites rs_2$, then
|
|
950 |
we have the following equivalence hold:
|
|
951 |
\begin{itemize}
|
|
952 |
\item
|
|
953 |
$\sum rs_1 \sequal \sum rs_2$
|
|
954 |
\item
|
|
955 |
$\rsimpalts \; rs_1 \sequal \rsimpalts \; rs_2$
|
|
956 |
\end{itemize}
|
|
957 |
\end{lemma}
|
|
958 |
\noindent
|
|
959 |
Here are a few connecting lemmas showing that
|
|
960 |
if a list of regular expressions can be rewritten using $\grewrites$ or $\frewrites $ or
|
|
961 |
$\scfrewrites$,
|
|
962 |
then an alternative constructor taking the list can also be rewritten using $\hrewrites$:
|
|
963 |
\begin{lemma}
|
|
964 |
\begin{itemize}
|
|
965 |
\item
|
|
966 |
If $rs \grewrites rs'$ then $\sum rs \hrewrites \sum rs'$.
|
|
967 |
\item
|
|
968 |
If $rs \grewrites rs'$ then $\sum rs \hrewrites \rsimpalts \; rs'$
|
|
969 |
\item
|
|
970 |
If $rs_1 \scfrewrites rs_2$ then $\sum (rs @ rs_1) \hrewrites \sum (rs @ rs_2)$
|
|
971 |
\item
|
|
972 |
If $rs_1 \scfrewrites rs_2$ then $\sum rs_1 \hrewrites \sum rs_2$
|
|
973 |
|
|
974 |
\end{itemize}
|
|
975 |
\end{lemma}
|
|
976 |
\noindent
|
|
977 |
Here comes the meat of the proof,
|
|
978 |
which says that once two lists are rewritable to each other,
|
|
979 |
then they are equivalent under $\rsimp{}$:
|
|
980 |
\begin{lemma}
|
|
981 |
If $r_1 \hrewrites r_2$ then $r_1 \sequal r_2$.
|
|
982 |
\end{lemma}
|
|
983 |
|
|
984 |
\noindent
|
|
985 |
And similar to \ref{Bitcoded2} one can preserve rewritability after taking derivative
|
|
986 |
of two regular expressions on both sides:
|
|
987 |
\begin{lemma}\label{interleave}
|
|
988 |
If $r \hrewrites r' $ then $\rder{c}{r} \hrewrites \rder{c}{r'}$
|
|
989 |
\end{lemma}
|
|
990 |
\noindent
|
|
991 |
This allows proving more $\mathbf{rsimp}$-equivalent terms, involving $\backslash_r$ now.
|
|
992 |
\begin{lemma}\label{insideSimpRemoval}
|
|
993 |
$\rsimp{\rder{c}{\rsimp{r}}} = \rsimp{\rder{c}{r}} $
|
|
994 |
\end{lemma}
|
|
995 |
\noindent
|
|
996 |
\begin{proof}
|
|
997 |
By \ref{interleave} and \ref{rsimpIdem}.
|
|
998 |
\end{proof}
|
|
999 |
\noindent
|
|
1000 |
And this unlocks more equivalent terms:
|
|
1001 |
\begin{lemma}\label{Simpders}
|
|
1002 |
As corollaries of \ref{insideSimpRemoval}, we have
|
|
1003 |
\begin{itemize}
|
|
1004 |
\item
|
|
1005 |
If $s \neq []$ then $\rderssimp{r}{s} = \rsimp{(\rders \; r \; s)}$.
|
|
1006 |
\item
|
|
1007 |
$\rsimpalts \; (\map \; (\_ \backslash_r x) \;
|
|
1008 |
(\rdistinct{rs}{\varnothing})) \sequal
|
|
1009 |
\rsimpalts \; (\rDistinct \;
|
|
1010 |
(\map \; (\_ \backslash_r x) rs) \;\varnothing )$
|
|
1011 |
\end{itemize}
|
|
1012 |
\end{lemma}
|
|
1013 |
\noindent
|
|
1014 |
|
|
1015 |
Finally,
|
|
1016 |
together with
|
|
1017 |
\begin{lemma}\label{rderRsimpAltsCommute}
|
|
1018 |
$\rder{x}{(\rsimpalts \; rs)} = \rsimpalts \; (\map \; (\rder{x}{\_}) \; rs)$
|
|
1019 |
\end{lemma}
|
|
1020 |
\noindent
|
|
1021 |
this leads to the first closed form--
|
|
1022 |
\begin{lemma}\label{altsClosedForm}
|
556
|
1023 |
\begin{center}
|
557
|
1024 |
$\rderssimp{(\sum rs)}{s} \sequal
|
|
1025 |
\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
|
556
|
1026 |
\end{center}
|
557
|
1027 |
\end{lemma}
|
556
|
1028 |
|
|
1029 |
\noindent
|
557
|
1030 |
\begin{proof}
|
|
1031 |
By a reverse induction on the string $s$.
|
|
1032 |
One rewriting step, as we mentioned earlier,
|
|
1033 |
involves
|
|
1034 |
\begin{center}
|
|
1035 |
$\rsimpalts \; (\map \; (\_ \backslash x) \;
|
|
1036 |
(\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \;
|
|
1037 |
(\lambda r. \rderssimp{r}{xs}))))}{\varnothing}))
|
|
1038 |
\sequal
|
|
1039 |
\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \;
|
|
1040 |
(\rflts \; (\map \; (\rsimp{} \; \circ \;
|
|
1041 |
(\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}) $.
|
|
1042 |
\end{center}
|
|
1043 |
This can be proven by a combination of
|
|
1044 |
\ref{grewritesSimpalts}, \ref{gstarRdistinctGeneral}, \ref{rderRsimpAltsCommute}, and
|
|
1045 |
\ref{insideSimpRemoval}.
|
|
1046 |
\end{proof}
|
|
1047 |
\noindent
|
|
1048 |
This closed form has a variant which can be more convenient in later proofs:
|
559
|
1049 |
\begin{corollary}{altsClosedForm1}
|
557
|
1050 |
If $s \neq []$ then
|
|
1051 |
$\rderssimp \; (\sum \; rs) \; s =
|
|
1052 |
\rsimp{(\sum \; (\map \; \rderssimp{\_}{s} \; rs))}$.
|
|
1053 |
\end{corollary}
|
|
1054 |
\noindent
|
|
1055 |
The harder closed forms are the sequence and star ones.
|
|
1056 |
Before we go on to obtain them, some preliminary definitions
|
|
1057 |
are needed to make proof statements concise.
|
556
|
1058 |
|
558
|
1059 |
\section{"Closed Forms" of Sequence Regular Expressions}
|
|
1060 |
The problem of obataining a closed-form for sequence regular expression
|
|
1061 |
is constructing $(r_1 \cdot r_2) \backslash_r s$
|
|
1062 |
if we are only allowed to use a combination of $r_1 \backslash s''$
|
|
1063 |
and $r_2 \backslash s''$ , where $s''$ is from $s$.
|
|
1064 |
First let's look at a series of derivatives steps on a sequence
|
|
1065 |
regular expression, assuming that each time the first
|
|
1066 |
component of the sequence is always nullable):
|
557
|
1067 |
\begin{center}
|
558
|
1068 |
|
|
1069 |
$r_1 \cdot r_2 \quad \longrightarrow_{\backslash c} \quad r_1 \backslash c \cdot r_2 + r_2 \backslash c \quad \longrightarrow_{\backslash c'} \quad (r_1 \backslash cc' \cdot r_2 + r_2 \backslash c') + r_2 \backslash cc' \longrightarrow_{\backslash c''} \quad$\\
|
|
1070 |
$((r_1 \backslash cc'c'' \cdot r_2 + r_2 \backslash c'') + r_2 \backslash c'c'') + r_2 \backslash cc'c'' \longrightarrow_{\backslash c''} \quad
|
|
1071 |
\ldots$
|
|
1072 |
|
557
|
1073 |
\end{center}
|
558
|
1074 |
Roughly speaking $r_1 \cdot r_2 \backslash s$ can be expresssed as
|
|
1075 |
a giant alternative taking a list of terms
|
|
1076 |
$[r_1 \backslash_r s \cdot r_2, r_2 \backslash_r s'', r_2 \backslash_r s_1'', \ldots]$,
|
|
1077 |
where the head of the list is always the term
|
|
1078 |
representing a match involving only $r_1$, and the tail of the list consisting of
|
|
1079 |
terms of the shape $r_2 \backslash_r s''$, $s''$ being a suffix of $s$.
|
557
|
1080 |
This intuition is also echoed by IndianPaper, where they gave
|
|
1081 |
a pencil-and-paper derivation of $(r_1 \cdot r_2)\backslash s$:
|
532
|
1082 |
\begin{center}
|
558
|
1083 |
\begin{tabular}{c}
|
|
1084 |
$(r_1 \cdot r_2) \backslash_r (c_1 :: c_2 :: \ldots c_n) \myequiv$\\
|
|
1085 |
\rule{0pt}{3ex} $((r_1 \backslash_r c_1) \cdot r_2 + (\delta\; (\rnullable \; r_1) \; r_2 \backslash_r c_1)) \backslash_r (c_2 :: \ldots c_n)
|
|
1086 |
\myequiv$\\
|
|
1087 |
\rule{0pt}{3ex} $((r_1 \backslash_r c_1c_2 \cdot r_2 + (\delta \; (\rnullable \; r_1) \; r_2 \backslash_r c_1c_2))
|
|
1088 |
+ (\delta \ (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)) \backslash_r (c_3 \ldots c_n)
|
557
|
1089 |
$
|
558
|
1090 |
\end{tabular}
|
557
|
1091 |
\end{center}
|
|
1092 |
\noindent
|
558
|
1093 |
The equality in above should be interpretated
|
|
1094 |
as language equivalence.
|
|
1095 |
The $\delta$ function works similarly to that of
|
|
1096 |
a Kronecker delta function:
|
|
1097 |
\[ \delta \; b\; r\]
|
|
1098 |
will produce $r$
|
|
1099 |
if $b$ evaluates to true,
|
|
1100 |
and $\RZERO$ otherwise.
|
|
1101 |
Note that their formulation
|
|
1102 |
\[
|
|
1103 |
((r_1 \backslash_r \, c_1c_2 \cdot r_2 + (\delta \; (\rnullable) \; r_1, r_2 \backslash_r c_1c_2)
|
|
1104 |
+ (\delta \; (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)
|
|
1105 |
\]
|
|
1106 |
does not faithfully
|
|
1107 |
represent what the intermediate derivatives would actually look like
|
|
1108 |
when one or more intermediate results $r_1 \backslash s' \cdot r_2$ are not
|
|
1109 |
nullable in the head of the sequence.
|
|
1110 |
For example, when $r_1$ and $r_1 \backslash_r c_1$ are not nullable,
|
|
1111 |
the regular expression would not look like
|
|
1112 |
\[
|
|
1113 |
(r_1 \backslash_r c_1c_2 + \RZERO ) + \RZERO,
|
|
1114 |
\]
|
|
1115 |
but actually $r_1 \backslash_r c_1c_2$, the redundant $\RZERO$s will not be created in the
|
|
1116 |
first place.
|
|
1117 |
In a closed-form one would want to take into account this
|
|
1118 |
and generate the list of
|
|
1119 |
regular expressions $r_2 \backslash_r s''$ with
|
|
1120 |
string pairs $(s', s'')$ where $s'@s'' = s$ and
|
|
1121 |
$r_1 \backslash s'$ nullable.
|
|
1122 |
We denote the list consisting of such
|
|
1123 |
strings $s''$ as $\vsuf{s}{r_1}$.
|
|
1124 |
|
|
1125 |
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string:
|
|
1126 |
\begin{center}
|
|
1127 |
\begin{tabular}{lcl}
|
|
1128 |
$\vsuf{[]}{\_} $ & $=$ & $[]$\\
|
|
1129 |
$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} (\rnullable{r_1}) \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
|
|
1130 |
&& $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) }) $
|
|
1131 |
\end{tabular}
|
|
1132 |
\end{center}
|
|
1133 |
\noindent
|
|
1134 |
The list is sorted in the order $r_2\backslash s''$
|
|
1135 |
appears in $(r_1\cdot r_2)\backslash s$.
|
|
1136 |
In essence, $\vsuf{\_}{\_}$ is doing a
|
|
1137 |
"virtual derivative" of $r_1 \cdot r_2$, but instead of producing
|
|
1138 |
the entire result $(r_1 \cdot r_2) \backslash s$,
|
|
1139 |
it only stores all the strings $s''$ such that $r_2 \backslash s''$
|
|
1140 |
are occurring terms in $(r_1\cdot r_2)\backslash s$.
|
|
1141 |
|
|
1142 |
To make the closed form representation
|
|
1143 |
more straightforward,
|
|
1144 |
the flattetning function $\sflat{\_}$ is used to enable the transformation from
|
557
|
1145 |
a left-associative nested sequence of alternatives into
|
|
1146 |
a flattened list:
|
558
|
1147 |
\[
|
|
1148 |
\sum [r_1, r_2, r_3, \ldots] \stackrel{\sflat{\_}}{\rightarrow}
|
|
1149 |
(\ldots ((r_1 + r_2) + r_3) + \ldots)
|
|
1150 |
\]
|
|
1151 |
\noindent
|
|
1152 |
The definitions $\sflat{\_}$, $\sflataux{\_}$ are given below.
|
557
|
1153 |
\begin{center}
|
|
1154 |
\begin{tabular}{ccc}
|
|
1155 |
$\sflataux{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
|
|
1156 |
$\sflataux{\AALTS{ }{[]}}$ & $ = $ & $ []$\\
|
|
1157 |
$\sflataux r$ & $=$ & $ [r]$
|
532
|
1158 |
\end{tabular}
|
|
1159 |
\end{center}
|
|
1160 |
|
557
|
1161 |
\begin{center}
|
|
1162 |
\begin{tabular}{ccc}
|
|
1163 |
$\sflat{(\sum r :: rs)}$ & $=$ & $\sum (\sflataux{r} @ rs)$\\
|
|
1164 |
$\sflat{\sum []}$ & $ = $ & $ \sum []$\\
|
|
1165 |
$\sflat r$ & $=$ & $ r$
|
|
1166 |
\end{tabular}
|
|
1167 |
\end{center}
|
558
|
1168 |
\noindent
|
|
1169 |
$\sflataux{\_}$ breaks up nested alternative regexes
|
557
|
1170 |
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
|
558
|
1171 |
into a "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
|
557
|
1172 |
It will return the singleton list $[r]$ otherwise.
|
|
1173 |
$\sflat{\_}$ works the same as $\sflataux{\_}$, except that it keeps
|
|
1174 |
the output type a regular expression, not a list.
|
558
|
1175 |
$\sflataux{\_}$ and $\sflat{\_}$ are only recursive on the
|
|
1176 |
first element of the list.
|
|
1177 |
|
|
1178 |
With $\sflataux{}$ a preliminary to the closed form can be stated,
|
|
1179 |
where the derivative of $r_1 \cdot r_2 \backslash s$ can be
|
|
1180 |
flattened into a list whose head and tail meet the description
|
|
1181 |
we gave earlier.
|
|
1182 |
\begin{lemma}\label{seqSfau0}
|
|
1183 |
$\sflataux{\rders{(r_1 \cdot r_2) \backslash s }} = (r_1 \backslash_r s) \cdot r_2
|
|
1184 |
:: (\map \; (r_2 \backslash_r \_) \; (\textit{Suffix} \; s \; r1))$
|
|
1185 |
\end{lemma}
|
|
1186 |
\begin{proof}
|
|
1187 |
By an induction on the string $s$, where the inductive cases
|
|
1188 |
are split as $[]$ and $xs @ [x]$.
|
|
1189 |
Note the key identify holds:
|
|
1190 |
\[
|
|
1191 |
\map \; (r_2 \backslash_r \_) \; (\vsuf{[x]}{(r_1 \backslash_r xs)}) \;\; @ \;\;
|
|
1192 |
\map \; (\_ \backslash_r x) \; (\map \; (r_2 \backslash \_) \; (\vsuf{xs}{r_1}))
|
|
1193 |
\]
|
|
1194 |
=
|
|
1195 |
\[
|
|
1196 |
\map \; (r_2 \backslash_r \_) \; (\vsuf{xs @ [x]}{r_1})
|
|
1197 |
\]
|
|
1198 |
This enables the inductive case to go through.
|
|
1199 |
\end{proof}
|
|
1200 |
\noindent
|
|
1201 |
Note that this lemma does $\mathbf{not}$ depend on any
|
|
1202 |
specific definitions we used,
|
|
1203 |
allowing people investigating derivatives to get an alternative
|
|
1204 |
view of what $r_1 \cdot r_2$ is.
|
532
|
1205 |
|
558
|
1206 |
Now we are able to use this for the intuition that
|
|
1207 |
the different ways in which regular expressions are
|
|
1208 |
nested do not matter under $\rsimp{}$:
|
557
|
1209 |
\begin{center}
|
558
|
1210 |
$\rsimp{r} \stackrel{?}{\sequal} \rsimp{r'}$ if $r = \sum [r_1, r_2, r_3, \ldots]$
|
|
1211 |
and $r' =(\ldots ((r_1 + r_2) + r_3) + \ldots)$
|
557
|
1212 |
\end{center}
|
558
|
1213 |
Simply wrap with $\sum$ constructor and add
|
|
1214 |
simplifications to both sides of \ref{seqSfau0}
|
|
1215 |
and one gets
|
|
1216 |
\begin{corollary}\label{seqClosedFormGeneral}
|
|
1217 |
$\rsimp{\sflat{(r_1 \cdot r_2) \backslash s} }
|
|
1218 |
=\rsimp{(\sum ( (r_1 \backslash s) \cdot r_2 ::
|
|
1219 |
\map\; (r_2 \backslash \_) \; (\vsuf{s}{r_1})))}$
|
|
1220 |
\end{corollary}
|
|
1221 |
Together with the idempotency property of $\rsimp{}$ (lemma \ref{rsimpIdem}),
|
|
1222 |
it is possible to convert the above lemma to obtain a "closed form"
|
|
1223 |
for derivatives nested with simplification:
|
|
1224 |
\begin{lemma}\label{seqClosedForm}
|
|
1225 |
$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{(\sum ((r_1 \backslash s) \cdot r_2 )
|
|
1226 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1})))}$
|
|
1227 |
\end{lemma}
|
|
1228 |
\begin{proof}
|
|
1229 |
By a case analysis of string $s$.
|
|
1230 |
When $s$ is empty list, the rewrite is straightforward.
|
|
1231 |
When $s$ is a list, one could use the corollary \ref{seqSfau0},
|
|
1232 |
and lemma \ref{Simpders} to rewrite the left-hand-side.
|
|
1233 |
\end{proof}
|
|
1234 |
As a corollary for this closed form, one can estimate the size
|
|
1235 |
of the sequence derivative $r_1 \cdot r_2 \backslash_r s$ using
|
|
1236 |
an easier-to-handle expression:
|
|
1237 |
\begin{corollary}\label{seqEstimate1}
|
|
1238 |
\begin{center}
|
557
|
1239 |
|
558
|
1240 |
$\llbracket \rderssimp{(r_1 \cdot r_2)}{s} \rrbracket_r = \llbracket \rsimp{(\sum ((r_1 \backslash s) \cdot r_2 )
|
|
1241 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1})))} \rrbracket_r$
|
|
1242 |
|
|
1243 |
\end{center}
|
|
1244 |
\end{corollary}
|
|
1245 |
\noindent
|
|
1246 |
\subsection{Closed Forms for Star Regular Expressions}
|
|
1247 |
We use a similar technique as $r_1 \cdot r_2$ case,
|
|
1248 |
generating
|
|
1249 |
all possible sub-strings $s'$ of $s$
|
|
1250 |
such that $r\backslash s' \cdot r^*$ will appear
|
|
1251 |
as a term in $(r^*) \backslash s$.
|
|
1252 |
The first function we define is a single-step
|
|
1253 |
updating function $\starupdate$, which takes three arguments as input:
|
|
1254 |
the new character $c$ to take derivative with,
|
|
1255 |
the regular expression
|
|
1256 |
$r$ directly under the star $r^*$, and the
|
|
1257 |
list of strings $sSet$ for the derivative $r^* \backslash s$
|
|
1258 |
up til this point
|
|
1259 |
such that $(r^*) \backslash s = \sum_{s' \in sSet} (r\backslash s') \cdot r^*$
|
|
1260 |
(the equality is not exact, more on this later).
|
|
1261 |
\begin{center}
|
|
1262 |
\begin{tabular}{lcl}
|
|
1263 |
$\starupdate \; c \; r \; [] $ & $\dn$ & $[]$\\
|
|
1264 |
$\starupdate \; c \; r \; (s :: Ss)$ & $\dn$ & \\
|
|
1265 |
& & $\textit{if} \;
|
|
1266 |
(\rnullable \; (\rders \; r \; s))$ \\
|
|
1267 |
& & $\textit{then} \;\; (s @ [c]) :: [c] :: (
|
|
1268 |
\starupdate \; c \; r \; Ss)$ \\
|
|
1269 |
& & $\textit{else} \;\; (s @ [c]) :: (
|
|
1270 |
\starupdate \; c \; r \; Ss)$
|
|
1271 |
\end{tabular}
|
|
1272 |
\end{center}
|
|
1273 |
\noindent
|
|
1274 |
As a generalisation from characters to strings,
|
|
1275 |
$\starupdates$ takes a string instead of a character
|
|
1276 |
as the first input argument, and is otherwise the same
|
|
1277 |
as $\starupdate$.
|
|
1278 |
\begin{center}
|
|
1279 |
\begin{tabular}{lcl}
|
|
1280 |
$\starupdates \; [] \; r \; Ss$ & $=$ & $Ss$\\
|
|
1281 |
$\starupdates \; (c :: cs) \; r \; Ss$ & $=$ & $\starupdates \; cs \; r \; (
|
|
1282 |
\starupdate \; c \; r \; Ss)$
|
|
1283 |
\end{tabular}
|
|
1284 |
\end{center}
|
|
1285 |
\noindent
|
|
1286 |
For the star regular expression,
|
|
1287 |
its derivatives can be seen as a nested gigantic
|
|
1288 |
alternative similar to that of sequence regular expression's derivatives,
|
|
1289 |
and therefore need
|
|
1290 |
to be ``straightened out" as well.
|
|
1291 |
The function for this would be $\hflat{}$ and $\hflataux{}$.
|
|
1292 |
\begin{center}
|
|
1293 |
\begin{tabular}{lcl}
|
|
1294 |
$\hflataux{r_1 + r_2}$ & $\dn$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
|
|
1295 |
$\hflataux{r}$ & $\dn$ & $[r]$
|
|
1296 |
\end{tabular}
|
|
1297 |
\end{center}
|
557
|
1298 |
|
|
1299 |
\begin{center}
|
558
|
1300 |
\begin{tabular}{lcl}
|
|
1301 |
$\hflat{r_1 + r_2}$ & $\dn$ & $\sum (\hflataux {r_1} @ \hflataux {r_2}) $\\
|
|
1302 |
$\hflat{r}$ & $\dn$ & $r$
|
|
1303 |
\end{tabular}
|
|
1304 |
\end{center}
|
|
1305 |
\noindent
|
|
1306 |
%MAYBE TODO: introduce createdByStar
|
|
1307 |
We first introduce an inductive property
|
|
1308 |
for $\starupdate$ and $\hflataux{\_}$,
|
|
1309 |
it says if we do derivatives of $r^*$
|
|
1310 |
with a string that starts with $c$,
|
|
1311 |
then flatten it out,
|
|
1312 |
we obtain a list
|
|
1313 |
of the shape $\sum_{s' \in sSet} (r\backslash_r s') \cdot r^*$,
|
|
1314 |
where $sSet = \starupdates \; s \; r \; [[c]]$.
|
|
1315 |
\begin{lemma}\label{starHfauInduct}
|
|
1316 |
$\hflataux{(\rders{( (\rder{c}{r_0})\cdot(r_0^*))}{s})} =
|
|
1317 |
\map \; (\lambda s_1. (r_0 \backslash_r s_1) \cdot (r_0^*)) \;
|
|
1318 |
(\starupdates \; s \; r_0 \; [[c]])$
|
|
1319 |
\end{lemma}
|
|
1320 |
\begin{proof}
|
|
1321 |
By an induction on $s$, the inductive cases
|
|
1322 |
being $[]$ and $s@[c]$.
|
|
1323 |
\end{proof}
|
|
1324 |
\noindent
|
|
1325 |
Here is a corollary that states the lemma in
|
|
1326 |
a more intuitive way:
|
|
1327 |
\begin{corollary}
|
|
1328 |
$\hflataux{r^* \backslash_r (c::xs)} = \map \; (\lambda s. (r \backslash_r s) \cdot
|
|
1329 |
(r^*))\; (\starupdates \; c\; r\; [[c]])$
|
|
1330 |
\end{corollary}
|
|
1331 |
\noindent
|
|
1332 |
Note that this is also agnostic of the simplification
|
|
1333 |
function we defined, and is therefore of more general interest.
|
|
1334 |
|
|
1335 |
Now adding the $\rsimp{}$ bit for closed forms,
|
|
1336 |
we have
|
|
1337 |
\begin{lemma}
|
|
1338 |
$a :: rs \grewrites \hflataux{a} @ rs$
|
|
1339 |
\end{lemma}
|
|
1340 |
\noindent
|
|
1341 |
giving us
|
|
1342 |
\begin{lemma}\label{cbsHfauRsimpeq1}
|
|
1343 |
$\rsimp{a+b} = \rsimp{(\sum \hflataux{a} @ \hflataux{b})}$.
|
|
1344 |
\end{lemma}
|
|
1345 |
\noindent
|
|
1346 |
This yields
|
|
1347 |
\begin{lemma}\label{hfauRsimpeq2}
|
|
1348 |
$\rsimp{r} = \rsimp{(\sum \hflataux{r})}$
|
|
1349 |
\end{lemma}
|
|
1350 |
\noindent
|
|
1351 |
Together with the rewriting relation
|
|
1352 |
\begin{lemma}\label{starClosedForm6Hrewrites}
|
|
1353 |
$\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss
|
|
1354 |
\scfrewrites
|
|
1355 |
\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss$
|
|
1356 |
\end{lemma}
|
|
1357 |
\noindent
|
|
1358 |
We obtain the closed form for star regular expression:
|
|
1359 |
\begin{lemma}\label{starClosedForm}
|
|
1360 |
$\rderssimp{r^*}{c::s} =
|
|
1361 |
\rsimp{
|
|
1362 |
(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \;
|
|
1363 |
(\starupdates \; s\; r \; [[c]])
|
|
1364 |
)
|
|
1365 |
)
|
|
1366 |
}
|
|
1367 |
$
|
|
1368 |
\end{lemma}
|
|
1369 |
\begin{proof}
|
|
1370 |
By an induction on $s$.
|
|
1371 |
The lemmas \ref{rsimpIdem}, \ref{starHfauInduct}, and \ref{hfauRsimpeq2}
|
|
1372 |
are used.
|
|
1373 |
\end{proof}
|
|
1374 |
\section{Estimating the Closed Forms' sizes}
|
|
1375 |
We now summarize the closed forms below:
|
|
1376 |
\begin{itemize}
|
|
1377 |
\item
|
|
1378 |
$\rderssimp{(\sum rs)}{s} \sequal
|
|
1379 |
\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
|
|
1380 |
\item
|
|
1381 |
$\rderssimp{(r_1 \cdot r_2)}{s} \sequal \sum ((r_1 \backslash s) \cdot r_2 )
|
|
1382 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1}))$
|
|
1383 |
\item
|
|
1384 |
|
|
1385 |
$\rderssimp{r^*}{c::s} =
|
|
1386 |
\rsimp{
|
|
1387 |
(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \;
|
|
1388 |
(\starupdates \; s\; r \; [[c]])
|
|
1389 |
)
|
|
1390 |
)
|
|
1391 |
}
|
|
1392 |
$
|
|
1393 |
\end{itemize}
|
|
1394 |
\noindent
|
|
1395 |
The closed forms on the left-hand-side
|
|
1396 |
are all of the same shape: $\rsimp{ (\sum rs)} $.
|
|
1397 |
Such regular expression will be bounded by the size of $\sum rs'$,
|
|
1398 |
where every element in $rs'$ is distinct, and each element
|
|
1399 |
can be described by some inductive sub-structures
|
|
1400 |
(for example when $r = r_1 \cdot r_2$ then $rs'$
|
|
1401 |
will be solely comprised of $r_1 \backslash s'$
|
|
1402 |
and $r_2 \backslash s''$, $s'$ and $s''$ being
|
|
1403 |
sub-strings of $s$).
|
|
1404 |
which will each have a size uppder bound
|
|
1405 |
according to inductive hypothesis, which controls $r \backslash s$.
|
557
|
1406 |
|
558
|
1407 |
We elaborate the above reasoning by a series of lemmas
|
|
1408 |
below, where straightforward proofs are omitted.
|
532
|
1409 |
\begin{lemma}
|
558
|
1410 |
If $\forall r \in rs. \rsize{r} $ is less than or equal to $N$,
|
|
1411 |
and $\textit{length} \; rs$ is less than or equal to $l$,
|
|
1412 |
then $\rsize{\sum rs}$ is less than or equal to $l*N + 1$.
|
|
1413 |
\end{lemma}
|
|
1414 |
\noindent
|
|
1415 |
If we define all regular expressions with size no
|
|
1416 |
more than $N$ as $\sizeNregex \; N$:
|
|
1417 |
\[
|
|
1418 |
\sizeNregex \; N \dn \{r \mid \rsize{r} \leq N \}
|
|
1419 |
\]
|
|
1420 |
Then such set is finite:
|
|
1421 |
\begin{lemma}\label{finiteSizeN}
|
|
1422 |
$\textit{isFinite}\; (\sizeNregex \; N)$
|
|
1423 |
\end{lemma}
|
|
1424 |
\begin{proof}
|
|
1425 |
By overestimating the set $\sizeNregex \; N + 1$
|
|
1426 |
using union of sets like
|
|
1427 |
$\{r_1 \cdot r_2 \mid r_1 \in A
|
|
1428 |
\text{and}
|
|
1429 |
r_2 \in A\}
|
|
1430 |
$ where $A = \sizeNregex \; N$.
|
|
1431 |
\end{proof}
|
|
1432 |
\noindent
|
|
1433 |
From this we get a corollary that
|
|
1434 |
if forall $r \in rs$, $\rsize{r} \leq N$, then the output of
|
|
1435 |
$\rdistinct{rs}{\varnothing}$ is a list of regular
|
|
1436 |
expressions of finite size depending on $N$ only.
|
|
1437 |
\begin{corollary}
|
|
1438 |
Assumes that for all $r \in rs. \rsize{r} \leq N$,
|
|
1439 |
and the cardinality of $\sizeNregex \; N$ is $c_N$
|
|
1440 |
then$\rsize{\rdistinct{rs}{\varnothing}} \leq c*N$.
|
|
1441 |
\end{corollary}
|
|
1442 |
\noindent
|
|
1443 |
We have proven that the output of $\rdistinct{rs'}{\varnothing}$
|
|
1444 |
is bounded by a constant $c_N$ depending only on $N$,
|
|
1445 |
provided that each of $rs'$'s element
|
|
1446 |
is bounded by $N$.
|
|
1447 |
We want to apply it to our setting $\rsize{\rsimp{\sum rs}}$.
|
|
1448 |
|
|
1449 |
We show how $\rdistinct$ and $\rflts$
|
|
1450 |
in the simplification function together is at least as
|
|
1451 |
good as $\rdistinct{}{}$ alone.
|
|
1452 |
\begin{lemma}\label{interactionFltsDB}
|
|
1453 |
$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r
|
|
1454 |
\leq
|
|
1455 |
\llbracket \rdistinct{rs}{\varnothing} \rrbracket_r $.
|
532
|
1456 |
\end{lemma}
|
558
|
1457 |
\noindent
|
|
1458 |
The intuition is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of
|
|
1459 |
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$.
|
|
1460 |
|
|
1461 |
Now this $\rsimp{\sum rs}$ can be estimated using $\rdistinct{rs}{\varnothing}$:
|
|
1462 |
\begin{lemma}\label{altsSimpControl}
|
|
1463 |
$\rsize{\rsimp{\sum rs}} \leq \rsize{\rdistinct{rs}{\varnothing}}+ 1$
|
532
|
1464 |
\end{lemma}
|
558
|
1465 |
\begin{proof}
|
|
1466 |
By using \ref{interactionFltsDB}.
|
|
1467 |
\end{proof}
|
|
1468 |
\noindent
|
|
1469 |
which says that the size of regular expression
|
|
1470 |
is always smaller if we apply the full simplification
|
|
1471 |
rather than just one component ($\rdistinct{}{}$).
|
|
1472 |
|
|
1473 |
|
|
1474 |
Now we are ready to control the sizes of
|
|
1475 |
$r_1 \cdot r_2 \backslash s$, $r^* \backslash s$.
|
|
1476 |
\begin{theorem}
|
|
1477 |
For any regex $r$, $\exists N_r. \forall s. \; \rsize{\rderssimp{r}{s}} \leq N_r$
|
|
1478 |
\end{theorem}
|
|
1479 |
\noindent
|
|
1480 |
\begin{proof}
|
|
1481 |
We prove this by induction on $r$. The base cases for $\RZERO$,
|
|
1482 |
$\RONE $ and $\RCHAR{c}$ are straightforward.
|
|
1483 |
In the sequence $r_1 \cdot r_2$ case,
|
|
1484 |
the inductive hypotheses state $\exists N_1. \forall s. \; \llbracket \rderssimp{r}{s} \rrbracket \leq N_1$ and
|
|
1485 |
$\exists N_2. \forall s. \; \llbracket \rderssimp{r_2}{s} \rrbracket \leq N_2$. We can reason as follows
|
|
1486 |
%
|
|
1487 |
\begin{center}
|
|
1488 |
\begin{tabular}{lcll}
|
|
1489 |
& & $ \llbracket \rderssimp{r_1\cdot r_2 }{s} \rrbracket_r $\\
|
|
1490 |
& $ = $ & $\llbracket \rsimp{(\sum(r_1 \backslash s \cdot r_2 \; \; :: \; \;
|
|
1491 |
\map \; (\lambda s'. r_2\backslash_{rsimp} s') (\vsuf{s}{r})))} \rrbracket_r $ & (1) \\
|
|
1492 |
& $\leq$ & $\llbracket \rsimp{(\sum(r_1 \backslash s \cdot r_2 ::
|
|
1493 |
\map \; (\lambda s'. r_2\backslash_{rsimp} s') (\vsuf{s}{r})))} \rrbracket_r $ & (2) \\
|
|
1494 |
|
|
1495 |
\end{tabular}
|
|
1496 |
\end{center}
|
|
1497 |
|
|
1498 |
\end{proof}
|
|
1499 |
|
|
1500 |
\noindent
|
559
|
1501 |
(1) is by the corollary \ref{seqEstimate1}
|
558
|
1502 |
The term (2) is used to control (1).
|
|
1503 |
That is because one can obtain an overall
|
|
1504 |
smaller regex list
|
|
1505 |
by flattening it and removing $\ZERO$s first before applying $\distinctWith$ on it.
|
|
1506 |
Section 3 is dedicated to its proof.
|
|
1507 |
In (3) we know that $\llbracket \ASEQ{bs}{(\rderssimp{ r_1}{s}}{r_2}\rrbracket$ is
|
|
1508 |
bounded by $N_1 + \llbracket{}r_2\rrbracket + 1$. In (5) we know the list comprehension contains only regular expressions of size smaller
|
|
1509 |
than $N_2$. The list length after $\distinctWith$ is bounded by a number, which we call $l_{N_2}$. It stands
|
|
1510 |
for the number of distinct regular expressions smaller than $N_2$ (there can only be finitely many of them).
|
|
1511 |
We reason similarly for $\STAR$.\medskip
|
|
1512 |
|
|
1513 |
%-----------------------------------
|
|
1514 |
% SECTION 2
|
|
1515 |
%-----------------------------------
|
|
1516 |
|
532
|
1517 |
|
557
|
1518 |
%----------------------------------------------------------------------------------------
|
|
1519 |
% SECTION 3
|
|
1520 |
%----------------------------------------------------------------------------------------
|
|
1521 |
|
532
|
1522 |
|
554
|
1523 |
\subsection{A Closed Form for the Sequence Regular Expression}
|
|
1524 |
\noindent
|
|
1525 |
|
|
1526 |
Before we get to the proof that says the intermediate result of our lexer will
|
|
1527 |
remain finitely bounded, which is an important efficiency/liveness guarantee,
|
|
1528 |
we shall first develop a few preparatory properties and definitions to
|
|
1529 |
make the process of proving that a breeze.
|
|
1530 |
|
|
1531 |
We define rewriting relations for $\rrexp$s, which allows us to do the
|
|
1532 |
same trick as we did for the correctness proof,
|
|
1533 |
but this time we will have stronger equalities established.
|
|
1534 |
|
532
|
1535 |
|
|
1536 |
|
|
1537 |
What guarantee does this bound give us?
|
|
1538 |
|
|
1539 |
Whatever the regex is, it will not grow indefinitely.
|
|
1540 |
Take our previous example $(a + aa)^*$ as an example:
|
|
1541 |
\begin{center}
|
|
1542 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
1543 |
\begin{tikzpicture}
|
|
1544 |
\begin{axis}[
|
|
1545 |
xlabel={number of $a$'s},
|
|
1546 |
x label style={at={(1.05,-0.05)}},
|
|
1547 |
ylabel={regex size},
|
|
1548 |
enlargelimits=false,
|
|
1549 |
xtick={0,5,...,30},
|
|
1550 |
xmax=33,
|
|
1551 |
ymax= 40,
|
|
1552 |
ytick={0,10,...,40},
|
|
1553 |
scaled ticks=false,
|
|
1554 |
axis lines=left,
|
|
1555 |
width=5cm,
|
|
1556 |
height=4cm,
|
|
1557 |
legend entries={$(a + aa)^*$},
|
|
1558 |
legend pos=north west,
|
|
1559 |
legend cell align=left]
|
|
1560 |
\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
|
|
1561 |
\end{axis}
|
|
1562 |
\end{tikzpicture}
|
|
1563 |
\end{tabular}
|
|
1564 |
\end{center}
|
|
1565 |
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
|
|
1566 |
with our simplification
|
|
1567 |
rules very effectively.
|
|
1568 |
|
|
1569 |
|
|
1570 |
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
|
|
1571 |
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
|
|
1572 |
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
|
|
1573 |
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
|
|
1574 |
$f(x) = x * 2^x$.
|
|
1575 |
This means the bound we have will surge up at least
|
|
1576 |
tower-exponentially with a linear increase of the depth.
|
|
1577 |
For a regex of depth $n$, the bound
|
|
1578 |
would be approximately $4^n$.
|
|
1579 |
|
|
1580 |
Test data in the graphs from randomly generated regular expressions
|
|
1581 |
shows that the giant bounds are far from being hit.
|
|
1582 |
%a few sample regular experessions' derivatives
|
|
1583 |
%size change
|
|
1584 |
%TODO: giving regex1_size_change.data showing a few regexes' size changes
|
|
1585 |
%w;r;t the input characters number, where the size is usually cubic in terms of original size
|
|
1586 |
%a*, aa*, aaa*, .....
|
|
1587 |
%randomly generated regexes
|
|
1588 |
\begin{center}
|
|
1589 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
1590 |
\begin{tikzpicture}
|
|
1591 |
\begin{axis}[
|
|
1592 |
xlabel={number of $a$'s},
|
|
1593 |
x label style={at={(1.05,-0.05)}},
|
|
1594 |
ylabel={regex size},
|
|
1595 |
enlargelimits=false,
|
|
1596 |
xtick={0,5,...,30},
|
|
1597 |
xmax=33,
|
|
1598 |
ymax=1000,
|
|
1599 |
ytick={0,100,...,1000},
|
|
1600 |
scaled ticks=false,
|
|
1601 |
axis lines=left,
|
|
1602 |
width=5cm,
|
|
1603 |
height=4cm,
|
|
1604 |
legend entries={regex1},
|
|
1605 |
legend pos=north west,
|
|
1606 |
legend cell align=left]
|
|
1607 |
\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
|
|
1608 |
\end{axis}
|
|
1609 |
\end{tikzpicture}
|
|
1610 |
&
|
|
1611 |
\begin{tikzpicture}
|
|
1612 |
\begin{axis}[
|
|
1613 |
xlabel={$n$},
|
|
1614 |
x label style={at={(1.05,-0.05)}},
|
|
1615 |
%ylabel={time in secs},
|
|
1616 |
enlargelimits=false,
|
|
1617 |
xtick={0,5,...,30},
|
|
1618 |
xmax=33,
|
|
1619 |
ymax=1000,
|
|
1620 |
ytick={0,100,...,1000},
|
|
1621 |
scaled ticks=false,
|
|
1622 |
axis lines=left,
|
|
1623 |
width=5cm,
|
|
1624 |
height=4cm,
|
|
1625 |
legend entries={regex2},
|
|
1626 |
legend pos=north west,
|
|
1627 |
legend cell align=left]
|
|
1628 |
\addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
|
|
1629 |
\end{axis}
|
|
1630 |
\end{tikzpicture}
|
|
1631 |
&
|
|
1632 |
\begin{tikzpicture}
|
|
1633 |
\begin{axis}[
|
|
1634 |
xlabel={$n$},
|
|
1635 |
x label style={at={(1.05,-0.05)}},
|
|
1636 |
%ylabel={time in secs},
|
|
1637 |
enlargelimits=false,
|
|
1638 |
xtick={0,5,...,30},
|
|
1639 |
xmax=33,
|
|
1640 |
ymax=1000,
|
|
1641 |
ytick={0,100,...,1000},
|
|
1642 |
scaled ticks=false,
|
|
1643 |
axis lines=left,
|
|
1644 |
width=5cm,
|
|
1645 |
height=4cm,
|
|
1646 |
legend entries={regex3},
|
|
1647 |
legend pos=north west,
|
|
1648 |
legend cell align=left]
|
|
1649 |
\addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
|
|
1650 |
\end{axis}
|
|
1651 |
\end{tikzpicture}\\
|
|
1652 |
\multicolumn{3}{c}{Graphs: size change of 3 randomly generated regexes $w.r.t.$ input string length.}
|
|
1653 |
\end{tabular}
|
|
1654 |
\end{center}
|
|
1655 |
|
|
1656 |
|
|
1657 |
|
|
1658 |
|
|
1659 |
|
|
1660 |
\noindent
|
|
1661 |
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the
|
|
1662 |
original size.
|
|
1663 |
This suggests a link towrads "partial derivatives"
|
|
1664 |
introduced by Antimirov \cite{Antimirov95}.
|
|
1665 |
|
|
1666 |
\section{Antimirov's partial derivatives}
|
|
1667 |
The idea behind Antimirov's partial derivatives
|
|
1668 |
is to do derivatives in a similar way as suggested by Brzozowski,
|
|
1669 |
but maintain a set of regular expressions instead of a single one:
|
|
1670 |
|
|
1671 |
%TODO: antimirov proposition 3.1, needs completion
|
|
1672 |
\begin{center}
|
|
1673 |
\begin{tabular}{ccc}
|
|
1674 |
$\partial_x(a+b)$ & $=$ & $\partial_x(a) \cup \partial_x(b)$\\
|
|
1675 |
$\partial_x(\ONE)$ & $=$ & $\phi$
|
|
1676 |
\end{tabular}
|
|
1677 |
\end{center}
|
|
1678 |
|
|
1679 |
Rather than joining the calculated derivatives $\partial_x a$ and $\partial_x b$ together
|
|
1680 |
using the alternatives constructor, Antimirov cleverly chose to put them into
|
|
1681 |
a set instead. This breaks the terms in a derivative regular expression up,
|
|
1682 |
allowing us to understand what are the "atomic" components of it.
|
|
1683 |
For example, To compute what regular expression $x^*(xx + y)^*$'s
|
|
1684 |
derivative against $x$ is made of, one can do a partial derivative
|
|
1685 |
of it and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
|
|
1686 |
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
|
|
1687 |
To get all the "atomic" components of a regular expression's possible derivatives,
|
|
1688 |
there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
|
|
1689 |
whatever character is available at the head of the string inside the language of a
|
|
1690 |
regular expression, and gives back the character and the derivative regular expression
|
|
1691 |
as a pair (which he called "monomial"):
|
|
1692 |
\begin{center}
|
|
1693 |
\begin{tabular}{ccc}
|
|
1694 |
$\lf(\ONE)$ & $=$ & $\phi$\\
|
|
1695 |
$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
|
|
1696 |
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
|
|
1697 |
$\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
|
|
1698 |
\end{tabular}
|
|
1699 |
\end{center}
|
|
1700 |
%TODO: completion
|
|
1701 |
|
|
1702 |
There is a slight difference in the last three clauses compared
|
|
1703 |
with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular
|
|
1704 |
expression $r$ with every element inside $\textit{rset}$ to create a set of
|
|
1705 |
sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates
|
|
1706 |
on a set of monomials (which Antimirov called "linear form") and a regular
|
|
1707 |
expression, and returns a linear form:
|
|
1708 |
\begin{center}
|
|
1709 |
\begin{tabular}{ccc}
|
|
1710 |
$l \bigodot (\ZERO)$ & $=$ & $\phi$\\
|
|
1711 |
$l \bigodot (\ONE)$ & $=$ & $l$\\
|
|
1712 |
$\phi \bigodot t$ & $=$ & $\phi$\\
|
|
1713 |
$\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
|
|
1714 |
$\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
|
|
1715 |
$\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
|
|
1716 |
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
|
|
1717 |
$\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
|
|
1718 |
\end{tabular}
|
|
1719 |
\end{center}
|
|
1720 |
%TODO: completion
|
|
1721 |
|
|
1722 |
Some degree of simplification is applied when doing $\bigodot$, for example,
|
|
1723 |
$l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
|
|
1724 |
and $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
|
|
1725 |
$\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
|
|
1726 |
and so on.
|
|
1727 |
|
|
1728 |
With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regex $r$ with
|
|
1729 |
an iterative procedure:
|
|
1730 |
\begin{center}
|
|
1731 |
\begin{tabular}{llll}
|
|
1732 |
$\textit{while}$ & $(\Delta_i \neq \phi)$ & & \\
|
|
1733 |
& $\Delta_{i+1}$ & $ =$ & $\lf(\Delta_i) - \PD_i$ \\
|
|
1734 |
& $\PD_{i+1}$ & $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
|
|
1735 |
$\partial_{UNIV}(r)$ & $=$ & $\PD$ &
|
|
1736 |
\end{tabular}
|
|
1737 |
\end{center}
|
|
1738 |
|
|
1739 |
|
|
1740 |
$(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
|
|
1741 |
|
|
1742 |
|
|
1743 |
However, if we introduce them in our
|
|
1744 |
setting we would lose the POSIX property of our calculated values.
|
|
1745 |
A simple example for this would be the regex $(a + a\cdot b)\cdot(b\cdot c + c)$.
|
|
1746 |
If we split this regex up into $a\cdot(b\cdot c + c) + a\cdot b \cdot (b\cdot c + c)$, the lexer
|
|
1747 |
would give back $\Left(\Seq(\Char(a), \Left(\Char(b \cdot c))))$ instead of
|
|
1748 |
what we want: $\Seq(\Right(ab), \Right(c))$. Unless we can store the structural information
|
|
1749 |
in all the places where a transformation of the form $(r_1 + r_2)\cdot r \rightarrow r_1 \cdot r + r_2 \cdot r$
|
|
1750 |
occurs, and apply them in the right order once we get
|
|
1751 |
a result of the "aggressively simplified" regex, it would be impossible to still get a $\POSIX$ value.
|
|
1752 |
This is unlike the simplification we had before, where the rewriting rules
|
|
1753 |
such as $\ONE \cdot r \rightsquigarrow r$, under which our lexer will give the same value.
|
|
1754 |
We will discuss better
|
|
1755 |
bounds in the last section of this chapter.\\[-6.5mm]
|
|
1756 |
|
|
1757 |
|
|
1758 |
|
|
1759 |
|
|
1760 |
%----------------------------------------------------------------------------------------
|
|
1761 |
% SECTION ??
|
|
1762 |
%----------------------------------------------------------------------------------------
|
|
1763 |
|
|
1764 |
%-----------------------------------
|
|
1765 |
% SECTION syntactic equivalence under simp
|
|
1766 |
%-----------------------------------
|
|
1767 |
\section{Syntactic Equivalence Under $\simp$}
|
|
1768 |
We prove that minor differences can be annhilated
|
|
1769 |
by $\simp$.
|
|
1770 |
For example,
|
|
1771 |
\begin{center}
|
|
1772 |
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
|
|
1773 |
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
|
|
1774 |
\end{center}
|
|
1775 |
|
|
1776 |
|
|
1777 |
%----------------------------------------------------------------------------------------
|
|
1778 |
% SECTION ALTS CLOSED FORM
|
|
1779 |
%----------------------------------------------------------------------------------------
|
|
1780 |
\section{A Closed Form for \textit{ALTS}}
|
|
1781 |
Now we prove that $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
|
|
1782 |
|
|
1783 |
|
|
1784 |
There are a few key steps, one of these steps is
|
556
|
1785 |
|
532
|
1786 |
|
|
1787 |
|
|
1788 |
One might want to prove this by something a simple statement like:
|
|
1789 |
|
|
1790 |
For this to hold we want the $\textit{distinct}$ function to pick up
|
|
1791 |
the elements before and after derivatives correctly:
|
|
1792 |
$r \in rset \equiv (rder x r) \in (rder x rset)$.
|
|
1793 |
which essentially requires that the function $\backslash$ is an injective mapping.
|
|
1794 |
|
|
1795 |
Unfortunately the function $\backslash c$ is not an injective mapping.
|
|
1796 |
|
|
1797 |
\subsection{function $\backslash c$ is not injective (1-to-1)}
|
|
1798 |
\begin{center}
|
|
1799 |
The derivative $w.r.t$ character $c$ is not one-to-one.
|
|
1800 |
Formally,
|
|
1801 |
$\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
|
|
1802 |
\end{center}
|
|
1803 |
This property is trivially true for the
|
|
1804 |
character regex example:
|
|
1805 |
\begin{center}
|
|
1806 |
$r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
|
|
1807 |
\end{center}
|
|
1808 |
But apart from the cases where the derivative
|
|
1809 |
output is $\ZERO$, are there non-trivial results
|
|
1810 |
of derivatives which contain strings?
|
|
1811 |
The answer is yes.
|
|
1812 |
For example,
|
|
1813 |
\begin{center}
|
|
1814 |
Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
|
|
1815 |
where $a$ is not nullable.\\
|
|
1816 |
$r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
|
|
1817 |
$r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
|
|
1818 |
\end{center}
|
|
1819 |
We start with two syntactically different regexes,
|
|
1820 |
and end up with the same derivative result.
|
|
1821 |
This is not surprising as we have such
|
|
1822 |
equality as below in the style of Arden's lemma:\\
|
|
1823 |
\begin{center}
|
|
1824 |
$L(A^*B) = L(A\cdot A^* \cdot B + B)$
|
|
1825 |
\end{center}
|
|
1826 |
|
|
1827 |
%----------------------------------------------------------------------------------------
|
|
1828 |
% SECTION 4
|
|
1829 |
%----------------------------------------------------------------------------------------
|
|
1830 |
\section{A Bound for the Star Regular Expression}
|
|
1831 |
We have shown how to control the size of the sequence regular expression $r_1\cdot r_2$ using
|
|
1832 |
the "closed form" of $(r_1 \cdot r_2) \backslash s$ and then
|
|
1833 |
the property of the $\distinct$ function.
|
|
1834 |
Now we try to get a bound on $r^* \backslash s$ as well.
|
|
1835 |
Again, we first look at how a star's derivatives evolve, if they grow maximally:
|
|
1836 |
\begin{center}
|
|
1837 |
|
|
1838 |
$r^* \quad \longrightarrow_{\backslash c} \quad (r\backslash c) \cdot r^* \quad \longrightarrow_{\backslash c'} \quad
|
|
1839 |
r \backslash cc' \cdot r^* + r \backslash c' \cdot r^* \quad \longrightarrow_{\backslash c''} \quad
|
|
1840 |
(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + (r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) \quad \longrightarrow_{\backslash c'''}
|
|
1841 |
\quad \ldots$
|
|
1842 |
|
|
1843 |
\end{center}
|
|
1844 |
When we have a string $s = c :: c' :: c'' \ldots$ such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$,
|
|
1845 |
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
|
|
1846 |
the number of terms in $r^* \backslash s$ will grow exponentially, causing the size
|
|
1847 |
of the derivatives $r^* \backslash s$ to grow exponentially, even if we do not
|
|
1848 |
count the possible size explosions of $r \backslash c$ themselves.
|
|
1849 |
|
|
1850 |
Thanks to $\flts$ and $\distinctWith$, we are able to open up regexes like
|
|
1851 |
$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + (r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $
|
|
1852 |
into $\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'', r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
|
|
1853 |
and then de-duplicate terms of the form $r\backslash s' \cdot r^*$ ($s'$ being a substring of $s$).
|
|
1854 |
For this we define $\hflataux{\_}$ and $\hflat{\_}$, similar to $\sflataux{\_}$ and $\sflat{\_}$:
|
|
1855 |
%TODO: definitions of and \hflataux \hflat
|
|
1856 |
\begin{center}
|
|
1857 |
\begin{tabular}{ccc}
|
|
1858 |
$\hflataux{r_1 + r_2}$ & $=$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
|
|
1859 |
$\hflataux r$ & $=$ & $ [r]$
|
|
1860 |
\end{tabular}
|
|
1861 |
\end{center}
|
|
1862 |
|
|
1863 |
\begin{center}
|
|
1864 |
\begin{tabular}{ccc}
|
|
1865 |
$\hflat{r_1 + r_2}$ & $=$ & $\RALTS{\hflataux{r_1} @ \hflataux{r_2}}$\\
|
|
1866 |
$\hflat r$ & $=$ & $ r$
|
|
1867 |
\end{tabular}
|
|
1868 |
\end{center}s
|
|
1869 |
Again these definitions are tailor-made for dealing with alternatives that have
|
|
1870 |
originated from a star's derivatives, so we don't attempt to open up all possible
|
|
1871 |
regexes of the form $\RALTS{rs}$, where $\textit{rs}$ might not contain precisely 2
|
|
1872 |
elements.
|
|
1873 |
We give a predicate for such "star-created" regular expressions:
|
|
1874 |
\begin{center}
|
|
1875 |
\begin{tabular}{lcr}
|
|
1876 |
& & $\createdByStar{(\RSEQ{ra}{\RSTAR{rb}}) }$\\
|
|
1877 |
$\createdByStar{r_1} \land \createdByStar{r_2} $ & $ \Longrightarrow$ & $\createdByStar{(r_1 + r_2)}$
|
|
1878 |
\end{tabular}
|
|
1879 |
\end{center}
|
|
1880 |
|
|
1881 |
These definitions allows us the flexibility to talk about
|
|
1882 |
regular expressions in their most convenient format,
|
|
1883 |
for example, flattened out $\RALTS{[r_1, r_2, \ldots, r_n]} $
|
|
1884 |
instead of binary-nested: $((r_1 + r_2) + (r_3 + r_4)) + \ldots$.
|
|
1885 |
These definitions help express that certain classes of syntatically
|
|
1886 |
distinct regular expressions are actually the same under simplification.
|
|
1887 |
This is not entirely true for annotated regular expressions:
|
|
1888 |
%TODO: bsimp bders \neq bderssimp
|
|
1889 |
\begin{center}
|
|
1890 |
$(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
|
|
1891 |
\end{center}
|
|
1892 |
For bit-codes, the order in which simplification is applied
|
|
1893 |
might cause a difference in the location they are placed.
|
|
1894 |
If we want something like
|
|
1895 |
\begin{center}
|
|
1896 |
$\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
|
|
1897 |
\end{center}
|
|
1898 |
Some "canonicalization" procedure is required,
|
|
1899 |
which either pushes all the common bitcodes to nodes
|
|
1900 |
as senior as possible:
|
|
1901 |
\begin{center}
|
|
1902 |
$_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
|
|
1903 |
\end{center}
|
|
1904 |
or does the reverse. However bitcodes are not of interest if we are talking about
|
|
1905 |
the $\llbracket r \rrbracket$ size of a regex.
|
|
1906 |
Therefore for the ease and simplicity of producing a
|
|
1907 |
proof for a size bound, we are happy to restrict ourselves to
|
|
1908 |
unannotated regular expressions, and obtain such equalities as
|
|
1909 |
\begin{lemma}
|
|
1910 |
$\rsimp{r_1 + r_2} = \rsimp{\RALTS{\hflataux{r_1} @ \hflataux{r_2}}}$
|
|
1911 |
\end{lemma}
|
|
1912 |
|
|
1913 |
\begin{proof}
|
|
1914 |
By using the rewriting relation $\rightsquigarrow$
|
|
1915 |
\end{proof}
|
|
1916 |
%TODO: rsimp sflat
|
|
1917 |
And from this we obtain a proof that a star's derivative will be the same
|
|
1918 |
as if it had all its nested alternatives created during deriving being flattened out:
|
|
1919 |
For example,
|
|
1920 |
\begin{lemma}
|
|
1921 |
$\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
|
|
1922 |
\end{lemma}
|
|
1923 |
\begin{proof}
|
|
1924 |
By structural induction on $r$, where the induction rules are these of $\createdByStar{_}$.
|
|
1925 |
\end{proof}
|
|
1926 |
% The simplification of a flattened out regular expression, provided it comes
|
|
1927 |
%from the derivative of a star, is the same as the one nested.
|
|
1928 |
|
|
1929 |
|
|
1930 |
|
|
1931 |
|
|
1932 |
|
|
1933 |
|
|
1934 |
|
|
1935 |
|
|
1936 |
|
|
1937 |
One might wonder the actual bound rather than the loose bound we gave
|
|
1938 |
for the convenience of an easier proof.
|
|
1939 |
How much can the regex $r^* \backslash s$ grow?
|
|
1940 |
As earlier graphs have shown,
|
|
1941 |
%TODO: reference that graph where size grows quickly
|
|
1942 |
they can grow at a maximum speed
|
|
1943 |
exponential $w.r.t$ the number of characters,
|
|
1944 |
but will eventually level off when the string $s$ is long enough.
|
|
1945 |
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
|
|
1946 |
would still be slow.
|
|
1947 |
And unfortunately, we have concrete examples
|
|
1948 |
where such regexes grew exponentially large before levelling off:
|
|
1949 |
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
|
|
1950 |
(\underbrace{a \ldots a}_{\text{n a's}})^*$ will already have a maximum
|
|
1951 |
size that is exponential on the number $n$
|
|
1952 |
under our current simplification rules:
|
|
1953 |
%TODO: graph of a regex whose size increases exponentially.
|
|
1954 |
\begin{center}
|
|
1955 |
\begin{tikzpicture}
|
|
1956 |
\begin{axis}[
|
|
1957 |
height=0.5\textwidth,
|
|
1958 |
width=\textwidth,
|
|
1959 |
xlabel=number of a's,
|
|
1960 |
xtick={0,...,9},
|
|
1961 |
ylabel=maximum size,
|
|
1962 |
ymode=log,
|
|
1963 |
log basis y={2}
|
|
1964 |
]
|
|
1965 |
\addplot[mark=*,blue] table {re-chengsong.data};
|
|
1966 |
\end{axis}
|
|
1967 |
\end{tikzpicture}
|
|
1968 |
\end{center}
|
|
1969 |
|
|
1970 |
For convenience we use $(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
|
|
1971 |
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
|
|
1972 |
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
|
|
1973 |
The exponential size is triggered by that the regex
|
|
1974 |
$\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
|
|
1975 |
inside the $(\ldots) ^*$ having exponentially many
|
|
1976 |
different derivatives, despite those difference being minor.
|
|
1977 |
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
|
|
1978 |
will therefore contain the following terms (after flattening out all nested
|
|
1979 |
alternatives):
|
|
1980 |
\begin{center}
|
|
1981 |
$(\oplus_{i = 1]{n} (\underbrace{a \ldots a}_{\text{((i - (m' \% i))\%i) a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* })\cdot (\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)$\\
|
|
1982 |
$(1 \leq m' \leq m )$
|
|
1983 |
\end{center}
|
|
1984 |
These terms are distinct for $m' \leq L.C.M.(1, \ldots, n)$ (will be explained in appendix).
|
|
1985 |
With each new input character taking the derivative against the intermediate result, more and more such distinct
|
|
1986 |
terms will accumulate,
|
|
1987 |
until the length reaches $L.C.M.(1, \ldots, n)$.
|
|
1988 |
$\textit{distinctBy}$ will not be able to de-duplicate any two of these terms
|
|
1989 |
$(\oplus_{i = 1}^{n} (\underbrace{a \ldots a}_{\text{((i - (m' \% i))\%i) a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* )\cdot (\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$\\
|
|
1990 |
|
|
1991 |
$(\oplus_{i = 1}^{n} (\underbrace{a \ldots a}_{\text{((i - (m'' \% i))\%i) a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* )\cdot (\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$\\
|
|
1992 |
where $m' \neq m''$ \\
|
|
1993 |
as they are slightly different.
|
|
1994 |
This means that with our current simplification methods,
|
|
1995 |
we will not be able to control the derivative so that
|
|
1996 |
$\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial %\leq O((\llbracket r\rrbacket)^c)$
|
|
1997 |
as there are already exponentially many terms.
|
|
1998 |
These terms are similar in the sense that the head of those terms
|
|
1999 |
are all consisted of sub-terms of the form:
|
|
2000 |
$(\underbrace{a \ldots a}_{\text{j a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* $.
|
|
2001 |
For $\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
|
|
2002 |
$n * (n + 1) / 2$ such terms.
|
|
2003 |
For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
|
|
2004 |
can be described by 6 terms:
|
|
2005 |
$a^*$, $a\cdot (aa)^*$, $ (aa)^*$,
|
|
2006 |
$aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
|
|
2007 |
The total number of different "head terms", $n * (n + 1) / 2$,
|
|
2008 |
is proportional to the number of characters in the regex
|
|
2009 |
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
|
|
2010 |
This suggests a slightly different notion of size, which we call the
|
|
2011 |
alphabetic width:
|
|
2012 |
%TODO:
|
|
2013 |
(TODO: Alphabetic width def.)
|
|
2014 |
|
|
2015 |
|
|
2016 |
Antimirov\parencite{Antimirov95} has proven that
|
|
2017 |
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$.
|
|
2018 |
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
|
|
2019 |
created by doing derivatives of $r$ against all possible strings.
|
|
2020 |
If we can make sure that at any moment in our lexing algorithm our
|
|
2021 |
intermediate result hold at most one copy of each of the
|
|
2022 |
subterms then we can get the same bound as Antimirov's.
|
|
2023 |
This leads to the algorithm in the next chapter.
|
|
2024 |
|
|
2025 |
|
|
2026 |
|
|
2027 |
|
|
2028 |
|
|
2029 |
%----------------------------------------------------------------------------------------
|
|
2030 |
% SECTION 1
|
|
2031 |
%----------------------------------------------------------------------------------------
|
|
2032 |
|
|
2033 |
|
|
2034 |
%-----------------------------------
|
|
2035 |
% SUBSECTION 1
|
|
2036 |
%-----------------------------------
|
|
2037 |
\subsection{Syntactic Equivalence Under $\simp$}
|
|
2038 |
We prove that minor differences can be annhilated
|
|
2039 |
by $\simp$.
|
|
2040 |
For example,
|
|
2041 |
\begin{center}
|
|
2042 |
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
|
|
2043 |
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
|
|
2044 |
\end{center}
|
|
2045 |
|