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 |
|
613
|
10 |
\begin{figure}
|
|
11 |
\begin{center}
|
|
12 |
\begin{tabular}{ccc}
|
|
13 |
$\llbracket _{bs}\ONE \rrbracket$ & $\dn$ & $1$\\
|
|
14 |
$\llbracket \ZERO \rrbracket$ & $\dn$ & $1$ \\
|
|
15 |
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket$ & $\dn$ & $\llbracket r_1 \rrbracket + \llbracket r_2 \rrbracket + 1$\\
|
|
16 |
$\llbracket _{bs}\mathbf{c} \rrbracket $ & $\dn$ & $1$\\
|
|
17 |
$\llbracket _{bs}\sum as \rrbracket $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket)\; as + 1$\\
|
|
18 |
$\llbracket _{bs} a^* \rrbracket $ & $\dn$ & $\llbracket a \rrbracket + 1$.
|
|
19 |
\end{tabular}
|
|
20 |
\end{center}
|
|
21 |
\caption{The size function of bitcoded regular expressions}\label{brexpSize}
|
|
22 |
\end{figure}
|
576
|
23 |
In this chapter we give a guarantee in terms of size:
|
590
|
24 |
given an annotated regular expression $a$, for any string $s$
|
|
25 |
our algorithm $\blexersimp$'s internal annotated regular expression
|
|
26 |
size is finitely bounded
|
|
27 |
by a constant $N_a$ that only depends on $a$:
|
576
|
28 |
\begin{center}
|
593
|
29 |
$\llbracket \bderssimp{a}{s} \rrbracket \leq N_a$
|
576
|
30 |
\end{center}
|
|
31 |
\noindent
|
577
|
32 |
where the size of an annotated regular expression is defined
|
613
|
33 |
in terms of the number of nodes in its tree structure (see figure \ref{brexpSize}).
|
|
34 |
We believe this size bound
|
|
35 |
is important in the context of POSIX lexing, because
|
590
|
36 |
\begin{itemize}
|
|
37 |
\item
|
|
38 |
It is a stepping stone towards an ``absence of catastrophic-backtracking''
|
613
|
39 |
guarantee.
|
|
40 |
If the internal data structures used by our algorithm cannot grow very large,
|
|
41 |
then our algorithm (which traverses those structures) cannot be too slow.
|
|
42 |
The next step would be to refine the bound $N_a$ so that it
|
590
|
43 |
is polynomial on $\llbracket a\rrbracket$.
|
|
44 |
\item
|
613
|
45 |
Having it formalised gives us a higher confidence that
|
590
|
46 |
our simplification algorithm $\simp$ does not ``mis-behave''
|
|
47 |
like $\simpsulz$ does.
|
|
48 |
The bound is universal, which is an advantage over work which
|
|
49 |
only gives empirical evidence on some test cases.
|
|
50 |
\end{itemize}
|
576
|
51 |
\section{Formalising About Size}
|
577
|
52 |
\noindent
|
613
|
53 |
In our lexer ($\blexersimp$),
|
|
54 |
we take an annotated regular expression as input,
|
|
55 |
and repeately take derivative of and simplify it:
|
590
|
56 |
\begin{figure}[H]
|
593
|
57 |
\begin{tikzpicture}[scale=2,
|
|
58 |
every node/.style={minimum size=11mm},
|
|
59 |
->,>=stealth',shorten >=1pt,auto,thick
|
|
60 |
]
|
|
61 |
\node (r0) [rectangle, draw=black, thick, minimum size = 5mm, draw=blue] {$a$};
|
|
62 |
\node (r1) [rectangle, draw=black, thick, right=of r0, minimum size = 7mm]{$a_1$};
|
|
63 |
\draw[->,line width=0.2mm](r0)--(r1) node[above,midway] {$\backslash c_1$};
|
590
|
64 |
|
593
|
65 |
\node (r1s) [rectangle, draw=blue, thick, right=of r1, minimum size=6mm]{$a_{1s}$};
|
|
66 |
\draw[->, line width=0.2mm](r1)--(r1s) node[above, midway] {$\simp$};
|
590
|
67 |
|
593
|
68 |
\node (r2) [rectangle, draw=black, thick, right=of r1s, minimum size = 12mm]{$a_2$};
|
|
69 |
\draw[->,line width=0.2mm](r1s)--(r2) node[above,midway] {$\backslash c_2$};
|
590
|
70 |
|
593
|
71 |
\node (r2s) [rectangle, draw = blue, thick, right=of r2,minimum size=6mm]{$a_{2s}$};
|
|
72 |
\draw[->,line width=0.2mm](r2)--(r2s) node[above,midway] {$\simp$};
|
590
|
73 |
|
593
|
74 |
\node (rns) [rectangle, draw = blue, thick, right=of r2s,minimum size=6mm]{$a_{ns}$};
|
|
75 |
\draw[->,line width=0.2mm, dashed](r2s)--(rns) node[above,midway] {$\backslash \ldots$};
|
590
|
76 |
|
593
|
77 |
\node (v) [circle, thick, draw, right=of rns, minimum size=6mm, right=1.7cm]{$v$};
|
|
78 |
\draw[->, line width=0.2mm](rns)--(v) node[above, midway] {\bmkeps} node [below, midway] {\decode};
|
|
79 |
\end{tikzpicture}
|
|
80 |
\caption{Regular expression size change during our $\blexersimp$ algorithm}\label{simpShrinks}
|
590
|
81 |
\end{figure}
|
576
|
82 |
\noindent
|
590
|
83 |
Each time
|
613
|
84 |
a derivative is taken, the regular expression might grow.
|
|
85 |
However, the simplification that is immediately afterwards will always shrink it so that
|
590
|
86 |
it stays small.
|
577
|
87 |
This intuition is depicted by the relative size
|
590
|
88 |
change between the black and blue nodes:
|
|
89 |
After $\simp$ the node always shrinks.
|
|
90 |
Our proof says that all the blue nodes
|
613
|
91 |
stay below a size bound $N_a$ determined by the input $a$.
|
576
|
92 |
|
590
|
93 |
\noindent
|
613
|
94 |
Sulzmann and Lu's assumed a similar picture about their algorithm,
|
590
|
95 |
though in fact their algorithm's size might be better depicted by the following graph:
|
|
96 |
\begin{figure}[H]
|
593
|
97 |
\begin{tikzpicture}[scale=2,
|
|
98 |
every node/.style={minimum size=11mm},
|
|
99 |
->,>=stealth',shorten >=1pt,auto,thick
|
|
100 |
]
|
|
101 |
\node (r0) [rectangle, draw=black, thick, minimum size = 5mm, draw=blue] {$a$};
|
|
102 |
\node (r1) [rectangle, draw=black, thick, right=of r0, minimum size = 7mm]{$a_1$};
|
|
103 |
\draw[->,line width=0.2mm](r0)--(r1) node[above,midway] {$\backslash c_1$};
|
590
|
104 |
|
593
|
105 |
\node (r1s) [rectangle, draw=blue, thick, right=of r1, minimum size=7mm]{$a_{1s}$};
|
|
106 |
\draw[->, line width=0.2mm](r1)--(r1s) node[above, midway] {$\simp'$};
|
590
|
107 |
|
593
|
108 |
\node (r2) [rectangle, draw=black, thick, right=of r1s, minimum size = 17mm]{$a_2$};
|
|
109 |
\draw[->,line width=0.2mm](r1s)--(r2) node[above,midway] {$\backslash c_2$};
|
590
|
110 |
|
593
|
111 |
\node (r2s) [rectangle, draw = blue, thick, right=of r2,minimum size=14mm]{$a_{2s}$};
|
|
112 |
\draw[->,line width=0.2mm](r2)--(r2s) node[above,midway] {$\simp'$};
|
590
|
113 |
|
593
|
114 |
\node (r3) [rectangle, draw = black, thick, right= of r2s, minimum size = 22mm]{$a_3$};
|
|
115 |
\draw[->,line width=0.2mm](r2s)--(r3) node[above,midway] {$\backslash c_3$};
|
590
|
116 |
|
593
|
117 |
\node (rns) [right = of r3, draw=blue, minimum size = 20mm]{$a_{3s}$};
|
|
118 |
\draw[->,line width=0.2mm] (r3)--(rns) node [above, midway] {$\simp'$};
|
590
|
119 |
|
593
|
120 |
\node (rnn) [right = of rns, minimum size = 1mm]{};
|
|
121 |
\draw[->, dashed] (rns)--(rnn) node [above, midway] {$\ldots$};
|
590
|
122 |
|
593
|
123 |
\end{tikzpicture}
|
|
124 |
\caption{Regular expression size change during our $\blexersimp$ algorithm}\label{sulzShrinks}
|
590
|
125 |
\end{figure}
|
|
126 |
\noindent
|
613
|
127 |
The picture means that on certain cases their lexer (where they use $\simpsulz$
|
|
128 |
as the simplification function)
|
576
|
129 |
will have an indefinite size explosion, causing the running time
|
613
|
130 |
of each derivative step to grow continuously (for example
|
590
|
131 |
in \ref{SulzmannLuLexerTime}).
|
613
|
132 |
They tested out the run time of their
|
590
|
133 |
lexer on particular examples such as $(a+b+ab)^*$
|
613
|
134 |
and claimed that their algorithm is linear w.r.t to the input.
|
|
135 |
With our mecahnised proof, we avoid this type of unintentional
|
|
136 |
generalisation.\\
|
|
137 |
|
|
138 |
Before delving in to the details of the formalisation,
|
|
139 |
we are going to provide an overview of it.
|
|
140 |
In the next subsection, we draw a picture of the bird's eye view
|
|
141 |
of the proof.
|
|
142 |
|
590
|
143 |
|
577
|
144 |
\subsection{Overview of the Proof}
|
613
|
145 |
Here is a bird's eye view of the main components of the finiteness proof,
|
577
|
146 |
which involves three steps:
|
593
|
147 |
\begin{figure}[H]
|
|
148 |
\begin{tikzpicture}[scale=1,font=\bf,
|
|
149 |
node/.style={
|
|
150 |
rectangle,rounded corners=3mm,
|
|
151 |
ultra thick,draw=black!50,minimum height=18mm,
|
|
152 |
minimum width=20mm,
|
|
153 |
top color=white,bottom color=black!20}]
|
543
|
154 |
|
|
155 |
|
593
|
156 |
\node (0) at (-5,0)
|
|
157 |
[node, text width=1.8cm, text centered]
|
|
158 |
{$\llbracket \bderssimp{a}{s} \rrbracket$};
|
|
159 |
\node (A) at (0,0)
|
|
160 |
[node,text width=1.6cm, text centered]
|
|
161 |
{$\llbracket \rderssimp{r}{s} \rrbracket_r$};
|
|
162 |
\node (B) at (3,0)
|
|
163 |
[node,text width=3.0cm, anchor=west, minimum width = 40mm]
|
|
164 |
{$\llbracket \textit{ClosedForm}(r, s)\rrbracket_r$};
|
|
165 |
\node (C) at (9.5,0) [node, minimum width=10mm] {$N_r$};
|
|
166 |
|
|
167 |
\draw [->,line width=0.5mm] (0) --
|
|
168 |
node [above,pos=0.45] {=} (A) node [below, pos = 0.45] {$(r = a \downarrow_r)$} (A);
|
|
169 |
\draw [->,line width=0.5mm] (A) --
|
|
170 |
node [above,pos=0.35] {$\quad =\ldots=$} (B);
|
|
171 |
\draw [->,line width=0.5mm] (B) --
|
|
172 |
node [above,pos=0.35] {$\quad \leq \ldots \leq$} (C);
|
|
173 |
\end{tikzpicture}
|
|
174 |
%\caption{
|
|
175 |
\end{figure}
|
576
|
176 |
\noindent
|
577
|
177 |
We explain the steps one by one:
|
532
|
178 |
\begin{itemize}
|
590
|
179 |
\item
|
|
180 |
We first introduce the operations such as
|
|
181 |
derivatives, simplification, size calculation, etc.
|
|
182 |
associated with $\rrexp$s, which we have given
|
|
183 |
a very brief introduction to in chapter \ref{Bitcoded2}.
|
593
|
184 |
The operations on $\rrexp$s are identical to those on
|
|
185 |
annotated regular expressions except that they are unaware
|
|
186 |
of bitcodes. This means that all proofs about size of $\rrexp$s will apply to
|
|
187 |
annotated regular expressions.
|
590
|
188 |
\item
|
593
|
189 |
We prove that $\rderssimp{r}{s} = \textit{ClosedForm}(r, s)$,
|
|
190 |
where $\textit{ClosedForm}(r, s)$ is entirely
|
|
191 |
written in the derivatives of their children regular
|
|
192 |
expressions.
|
|
193 |
We call the right-hand-side the \emph{Closed Form}
|
|
194 |
of the derivative $\rderssimp{r}{s}$.
|
590
|
195 |
\item
|
593
|
196 |
We estimate $\llbracket \textit{ClosedForm}(r, s) \rrbracket_r$.
|
|
197 |
The key observation is that $\distinctBy$'s output is
|
|
198 |
a list with a constant length bound.
|
532
|
199 |
\end{itemize}
|
594
|
200 |
We will expand on these steps in the next sections.\\
|
532
|
201 |
|
613
|
202 |
\section{The $\textit{Rrexp}$ Datatype}
|
594
|
203 |
The first step is to define
|
|
204 |
$\textit{rrexp}$s.
|
|
205 |
They are without bitcodes,
|
|
206 |
allowing a much simpler size bound proof.
|
|
207 |
Of course, the bits which encode the lexing information
|
|
208 |
would grow linearly with respect
|
|
209 |
to the input, which should be taken into account when we wish to tackle the runtime comlexity.
|
|
210 |
But for the sake of the structural size
|
|
211 |
we can safely ignore them.\\
|
|
212 |
To recapitulate, the datatype
|
|
213 |
definition of the $\rrexp$, called
|
593
|
214 |
\emph{r-regular expressions},
|
594
|
215 |
was initially defined in \ref{rrexpDef}.
|
|
216 |
The reason for the prefix $r$ is
|
593
|
217 |
to make a distinction
|
594
|
218 |
with basic regular expressions.
|
576
|
219 |
\[ \rrexp ::= \RZERO \mid \RONE
|
593
|
220 |
\mid \RCHAR{c}
|
|
221 |
\mid \RSEQ{r_1}{r_2}
|
|
222 |
\mid \RALTS{rs}
|
|
223 |
\mid \RSTAR{r}
|
576
|
224 |
\]
|
593
|
225 |
The size of an r-regular expression is
|
|
226 |
written $\llbracket r\rrbracket_r$,
|
|
227 |
whose definition mirrors that of an annotated regular expression.
|
576
|
228 |
\begin{center}
|
593
|
229 |
\begin{tabular}{ccc}
|
|
230 |
$\llbracket _{bs}\ONE \rrbracket_r$ & $\dn$ & $1$\\
|
|
231 |
$\llbracket \ZERO \rrbracket_r$ & $\dn$ & $1$ \\
|
|
232 |
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket_r$ & $\dn$ & $\llbracket r_1 \rrbracket_r + \llbracket r_2 \rrbracket_r + 1$\\
|
|
233 |
$\llbracket _{bs}\mathbf{c} \rrbracket_r $ & $\dn$ & $1$\\
|
|
234 |
$\llbracket _{bs}\sum as \rrbracket_r $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket_r)\; as + 1$\\
|
|
235 |
$\llbracket _{bs} a^* \rrbracket_r $ & $\dn$ & $\llbracket a \rrbracket_r + 1$.
|
|
236 |
\end{tabular}
|
576
|
237 |
\end{center}
|
|
238 |
\noindent
|
593
|
239 |
The $r$ in the subscript of $\llbracket \rrbracket_r$ is to
|
|
240 |
differentiate with the same operation for annotated regular expressions.
|
|
241 |
Adding $r$ as subscript will be used in
|
594
|
242 |
other operations as well.\\
|
593
|
243 |
The transformation from an annotated regular expression
|
|
244 |
to an r-regular expression is straightforward.
|
|
245 |
\begin{center}
|
|
246 |
\begin{tabular}{lcl}
|
|
247 |
$\rerase{\ZERO}$ & $\dn$ & $\RZERO$\\
|
|
248 |
$\rerase{_{bs}\ONE}$ & $\dn$ & $\RONE$\\
|
|
249 |
$\rerase{_{bs}\mathbf{c}}$ & $\dn$ & $\RCHAR{c}$\\
|
|
250 |
$\rerase{_{bs}r_1\cdot r_2}$ & $\dn$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
|
|
251 |
$\rerase{_{bs}\sum as}$ & $\dn$ & $\RALTS{\map \; \rerase{\_} \; as}$\\
|
|
252 |
$\rerase{_{bs} a ^*}$ & $\dn$ & $\rerase{a} ^*$
|
|
253 |
\end{tabular}
|
|
254 |
\end{center}
|
594
|
255 |
|
613
|
256 |
\subsection{Why a New Datatype?}
|
594
|
257 |
The reason we take all the trouble
|
|
258 |
defining a new datatype is that $\erase$ makes things harder.
|
576
|
259 |
We initially started by using
|
|
260 |
plain regular expressions and tried to prove
|
594
|
261 |
the lemma \ref{rsizeAsize},
|
|
262 |
however the $\erase$ function unavoidbly messes with the structure of the
|
|
263 |
annotated regular expression.
|
|
264 |
The $+$ constructor
|
|
265 |
of basic regular expressions is binary whereas $\sum$
|
|
266 |
takes a list, and one has to convert between them:
|
576
|
267 |
\begin{center}
|
594
|
268 |
\begin{tabular}{ccc}
|
|
269 |
$\erase \; _{bs}\sum [] $ & $\dn$ & $\ZERO$\\
|
|
270 |
$\erase \; _{bs}\sum [a]$ & $\dn$ & $a$\\
|
|
271 |
$\erase \; _{bs}\sum a :: as$ & $\dn$ & $a + (\erase \; _{[]} \sum as)\quad \text{if $as$ length over 1}$
|
|
272 |
\end{tabular}
|
576
|
273 |
\end{center}
|
594
|
274 |
\noindent
|
|
275 |
An alternative regular expression with an empty argument list
|
|
276 |
will be turned into a $\ZERO$.
|
|
277 |
The singleton alternative $\sum [r]$ would have $r$ during the
|
|
278 |
$\erase$ function.
|
|
279 |
The annotated regular expression $\sum[a, b, c]$ would turn into
|
|
280 |
$(a+(b+c))$.
|
|
281 |
All these operations change the size and structure of
|
|
282 |
an annotated regular expressions, adding unnecessary
|
|
283 |
complexities to the size bound proof.\\
|
613
|
284 |
For example, if we define the size of a basic plain regular expression
|
594
|
285 |
in the usual way,
|
543
|
286 |
\begin{center}
|
593
|
287 |
\begin{tabular}{ccc}
|
|
288 |
$\llbracket \ONE \rrbracket_p$ & $\dn$ & $1$\\
|
|
289 |
$\llbracket \ZERO \rrbracket_p$ & $\dn$ & $1$ \\
|
|
290 |
$\llbracket r_1 \cdot r_2 \rrbracket_p$ & $\dn$ & $\llbracket r_1 \rrbracket_p + \llbracket r_2 \rrbracket_p + 1$\\
|
|
291 |
$\llbracket \mathbf{c} \rrbracket_p $ & $\dn$ & $1$\\
|
|
292 |
$\llbracket r_1 \cdot r_2 \rrbracket_p $ & $\dn$ & $\llbracket r_1 \rrbracket_p \; + \llbracket r_2 \rrbracket_p + 1$\\
|
|
293 |
$\llbracket a^* \rrbracket_p $ & $\dn$ & $\llbracket a \rrbracket_p + 1$
|
|
294 |
\end{tabular}
|
532
|
295 |
\end{center}
|
543
|
296 |
\noindent
|
594
|
297 |
Then the property
|
532
|
298 |
\begin{center}
|
613
|
299 |
$\llbracket a \rrbracket \stackrel{?}{=} \llbracket a_\downarrow \rrbracket_p$
|
532
|
300 |
\end{center}
|
594
|
301 |
does not hold.
|
613
|
302 |
With $\textit{rerase}$, however,
|
|
303 |
only the bitcodes are thrown away.
|
|
304 |
Everything about the structure remains intact.
|
|
305 |
Therefore it does not change the size
|
|
306 |
of an annotated regular expression:
|
|
307 |
\begin{lemma}\label{rsizeAsize}
|
|
308 |
$\rsize{\rerase a} = \asize a$
|
|
309 |
\end{lemma}
|
|
310 |
\begin{proof}
|
|
311 |
By routine structural induction on $a$.
|
|
312 |
\end{proof}
|
|
313 |
\noindent
|
594
|
314 |
One might be able to prove an inequality such as
|
|
315 |
$\llbracket a \rrbracket \leq \llbracket a_\downarrow \rrbracket_p $
|
|
316 |
and then estimate $\llbracket a_\downarrow \rrbracket_p$,
|
|
317 |
but we found our approach more straightforward.\\
|
532
|
318 |
|
613
|
319 |
\subsection{Functions for R-regular Expressions}
|
|
320 |
We shall define the r-regular expression version
|
|
321 |
of $\blexer$ and $\blexersimp$ related functions.
|
|
322 |
We use $r$ as the prefix or subscript to differentiate
|
|
323 |
with the bitcoded version.
|
|
324 |
For example,$\backslash_r$, $\rdistincts$, and $\rsimp$
|
|
325 |
as opposed to $\backslash$, $\distinctBy$, and $\bsimp$.
|
|
326 |
As promised, they are much simpler than their bitcoded counterparts.
|
|
327 |
%The operations on r-regular expressions are
|
|
328 |
%almost identical to those of the annotated regular expressions,
|
|
329 |
%except that no bitcodes are used. For example,
|
|
330 |
The derivative operation becomes simpler:\\
|
543
|
331 |
\begin{center}
|
593
|
332 |
\begin{tabular}{@{}lcl@{}}
|
|
333 |
$(\ZERO)\,\backslash_r c$ & $\dn$ & $\ZERO$\\
|
|
334 |
$(\ONE)\,\backslash_r c$ & $\dn$ &
|
|
335 |
$\textit{if}\;c=d\; \;\textit{then}\;
|
|
336 |
\ONE\;\textit{else}\;\ZERO$\\
|
|
337 |
$(\sum \;\textit{rs})\,\backslash_r c$ & $\dn$ &
|
|
338 |
$\sum\;(\textit{map} \; (\_\backslash_r c) \; rs )$\\
|
|
339 |
$(r_1\cdot r_2)\,\backslash_r c$ & $\dn$ &
|
594
|
340 |
$\textit{if}\;(\textit{rnullable}\,r_1)$\\
|
593
|
341 |
& &$\textit{then}\;\sum\,[(r_1\,\backslash_r c)\cdot\,r_2,$\\
|
|
342 |
& &$\phantom{\textit{then},\;\sum\,}((r_2\,\backslash_r c))]$\\
|
|
343 |
& &$\textit{else}\;\,(r_1\,\backslash_r c)\cdot r_2$\\
|
|
344 |
$(r^*)\,\backslash_r c$ & $\dn$ &
|
|
345 |
$( r\,\backslash_r c)\cdot
|
|
346 |
(_{[]}r^*))$
|
|
347 |
\end{tabular}
|
543
|
348 |
\end{center}
|
|
349 |
\noindent
|
594
|
350 |
Similarly, $\distinctBy$ does not need
|
|
351 |
a function checking equivalence because
|
|
352 |
there are no bit annotations causing superficial differences
|
|
353 |
between syntactically equal terms.
|
532
|
354 |
\begin{center}
|
593
|
355 |
\begin{tabular}{lcl}
|
|
356 |
$\rdistinct{[]}{rset} $ & $\dn$ & $[]$\\
|
594
|
357 |
$\rdistinct{r :: rs}{rset}$ & $\dn$ &
|
|
358 |
$\textit{if}(r \in \textit{rset}) \; \textit{then} \; \rdistinct{rs}{rset}$\\
|
|
359 |
& & $\textit{else}\; \;
|
|
360 |
r::\rdistinct{rs}{(rset \cup \{r\})}$
|
593
|
361 |
\end{tabular}
|
532
|
362 |
\end{center}
|
|
363 |
%TODO: definition of rsimp (maybe only the alternative clause)
|
543
|
364 |
\noindent
|
595
|
365 |
We would like to make clear
|
|
366 |
a difference between our $\rdistincts$ and
|
|
367 |
the Isabelle $\textit {distinct}$ predicate.
|
|
368 |
In Isabelle $\textit{distinct}$ is a function that returns a boolean
|
|
369 |
rather than a list.
|
|
370 |
It tests if all the elements of a list are unique.\\
|
|
371 |
With $\textit{rdistinct}$,
|
|
372 |
and the flatten function for $\rrexp$s:
|
|
373 |
\begin{center}
|
|
374 |
\begin{tabular}{@{}lcl@{}}
|
596
|
375 |
$\textit{rflts} \; (\sum \textit{as}) :: \textit{as'}$ & $\dn$ & $as \; @ \; \textit{rflts} \; as' $ \\
|
595
|
376 |
$\textit{rflts} \; \ZERO :: as'$ & $\dn$ & $ \textit{rflts} \; \textit{as'} $ \\
|
|
377 |
$\textit{rflts} \; a :: as'$ & $\dn$ & $a :: \textit{rflts} \; \textit{as'}$ \quad(otherwise)
|
|
378 |
\end{tabular}
|
|
379 |
\end{center}
|
|
380 |
\noindent
|
|
381 |
one can chain together all the other modules
|
596
|
382 |
such as $\rsimpalts$:
|
|
383 |
\begin{center}
|
|
384 |
\begin{tabular}{@{}lcl@{}}
|
|
385 |
$\rsimpalts \;\; nil$ & $\dn$ & $\RZERO$\\
|
|
386 |
$\rsimpalts \;\; r::nil$ & $\dn$ & $r$\\
|
|
387 |
$\rsimpalts \;\; rs$ & $\dn$ & $\sum rs$\\
|
|
388 |
\end{tabular}
|
|
389 |
\end{center}
|
|
390 |
\noindent
|
|
391 |
and $\rsimpseq$:
|
|
392 |
\begin{center}
|
|
393 |
\begin{tabular}{@{}lcl@{}}
|
|
394 |
$\rsimpseq \;\; \RZERO \; \_ $ & $=$ & $\RZERO$\\
|
|
395 |
$\rsimpseq \;\; \_ \; \RZERO $ & $=$ & $\RZERO$\\
|
|
396 |
$\rsimpseq \;\; \RONE \cdot r_2$ & $\dn$ & $r_2$\\
|
|
397 |
$\rsimpseq \;\; r_1 r_2$ & $\dn$ & $r_1 \cdot r_2$\\
|
|
398 |
\end{tabular}
|
|
399 |
\end{center}
|
|
400 |
and get $\textit{rsimp}$ and $\rderssimp{\_}{\_}$:
|
595
|
401 |
\begin{center}
|
|
402 |
\begin{tabular}{@{}lcl@{}}
|
|
403 |
|
596
|
404 |
$\textit{rsimp} \; (r_1\cdot r_2)$ & $\dn$ & $ \textit{rsimp}_{SEQ} \; bs \;(\textit{rsimp} \; r_1) \; (\textit{rsimp} \; r_2) $ \\
|
|
405 |
$\textit{rsimp} \; (_{bs}\sum \textit{rs})$ & $\dn$ & $\textit{rsimp}_{ALTS} \; \textit{bs} \; (\textit{rdistinct} \; ( \textit{rflts} ( \textit{map} \; rsimp \; rs)) \; \rerases \; \varnothing) $ \\
|
|
406 |
$\textit{rsimp} \; r$ & $\dn$ & $\textit{r} \qquad \textit{otherwise}$
|
595
|
407 |
\end{tabular}
|
|
408 |
\end{center}
|
596
|
409 |
\begin{center}
|
|
410 |
\begin{tabular}{@{}lcl@{}}
|
|
411 |
$r\backslash_{rsimp} \, c$ & $\dn$ & $\rsimp \; (r\backslash_r \, c)$
|
|
412 |
\end{tabular}
|
|
413 |
\end{center}
|
|
414 |
|
|
415 |
\begin{center}
|
|
416 |
\begin{tabular}{@{}lcl@{}}
|
601
|
417 |
$r \backslash_{rsimps} \; \; c\!::\!s $ & $\dn$ & $(r \backslash_{rsimp}\, c) \backslash_{rsimps}\, s$ \\
|
596
|
418 |
$r \backslash_{rsimps} [\,] $ & $\dn$ & $r$
|
|
419 |
\end{tabular}
|
|
420 |
\end{center}
|
|
421 |
\noindent
|
601
|
422 |
We do not define an r-regular expression version of $\blexersimp$,
|
609
|
423 |
as our proof does not involve its use
|
|
424 |
(and there is no bitcode to decode into a lexical value).
|
613
|
425 |
Now we are ready to introduce how r-regular expressions allow
|
|
426 |
us to prove the size bound on bitcoded regular expressions.
|
|
427 |
|
|
428 |
\subsection{Using R-regular Expressions to Bound Bit-coded Regular Expressions}
|
|
429 |
Everything about the size of annotated regular expressions after the application
|
|
430 |
of function $\bsimp$ and $\backslash_{simps}$
|
|
431 |
can be calculated via the size of r-regular expressions after the application
|
|
432 |
of $\rsimp$ and $\backslash_{rsimps}$:
|
564
|
433 |
\begin{lemma}\label{sizeRelations}
|
553
|
434 |
The following equalities hold:
|
543
|
435 |
\begin{itemize}
|
|
436 |
\item
|
601
|
437 |
$\asize{\bsimps \; a} = \rsize{\rsimp{ \rerase{a}}}$
|
554
|
438 |
\item
|
596
|
439 |
$\asize{\bderssimp{a}{s}} = \rsize{\rderssimp{\rerase{a}}{s}}$
|
554
|
440 |
\end{itemize}
|
532
|
441 |
\end{lemma}
|
601
|
442 |
\begin{proof}
|
|
443 |
The first part is by induction on the inductive cases
|
|
444 |
of $\textit{bsimp}$.
|
|
445 |
The second part is by induction on the string $s$,
|
|
446 |
where the inductive step follows from part one.
|
|
447 |
\end{proof}
|
543
|
448 |
\noindent
|
596
|
449 |
With lemma \ref{sizeRelations},
|
601
|
450 |
we will be able to focus on
|
|
451 |
estimating only
|
|
452 |
$\rsize{\rderssimp{\rerase{a}}{s}}$
|
|
453 |
in later parts because
|
|
454 |
\begin{center}
|
|
455 |
$\rsize{\rderssimp{\rerase{a}}{s}} \leq N_r \quad$
|
|
456 |
implies
|
|
457 |
$\quad \llbracket a \backslash_{bsimps} s \rrbracket \leq N_r$.
|
|
458 |
\end{center}
|
613
|
459 |
From now on we
|
601
|
460 |
Unless stated otherwise in the rest of this
|
|
461 |
chapter all regular expressions without
|
609
|
462 |
bitcodes are seen as r-regular expressions ($\rrexp$s).
|
601
|
463 |
For the binary alternative r-regular expression $\RALTS{[r_1, r_2]}$,
|
|
464 |
we use the notation $r_1 + r_2$
|
|
465 |
for brevity.
|
532
|
466 |
|
|
467 |
|
|
468 |
%-----------------------------------
|
596
|
469 |
% SUB SECTION ROADMAP RREXP BOUND
|
532
|
470 |
%-----------------------------------
|
553
|
471 |
|
596
|
472 |
%\subsection{Roadmap to a Bound for $\textit{Rrexp}$}
|
553
|
473 |
|
596
|
474 |
%The way we obtain the bound for $\rrexp$s is by two steps:
|
|
475 |
%\begin{itemize}
|
|
476 |
% \item
|
|
477 |
% First, we rewrite $r\backslash s$ into something else that is easier
|
|
478 |
% to bound. This step is especially important for the inductive case
|
|
479 |
% $r_1 \cdot r_2$ and $r^*$, where the derivative can grow and bloat in a wild way,
|
|
480 |
% but after simplification they will always be equal or smaller to a form consisting of an alternative
|
|
481 |
% list of regular expressions $f \; (g\; (\sum rs))$ with some functions applied to it, where each element will be distinct after the function application.
|
|
482 |
% \item
|
|
483 |
% Then, for such a sum list of regular expressions $f\; (g\; (\sum rs))$, we can control its size
|
|
484 |
% by estimation, since $\distinctBy$ and $\flts$ are well-behaved and working together would only
|
|
485 |
% reduce the size of a regular expression, not adding to it.
|
|
486 |
%\end{itemize}
|
|
487 |
%
|
|
488 |
%\section{Step One: Closed Forms}
|
|
489 |
%We transform the function application $\rderssimp{r}{s}$
|
|
490 |
%into an equivalent
|
|
491 |
%form $f\; (g \; (\sum rs))$.
|
|
492 |
%The functions $f$ and $g$ can be anything from $\flts$, $\distinctBy$ and other helper functions from $\bsimp{\_}$.
|
|
493 |
%This way we get a different but equivalent way of expressing : $r\backslash s = f \; (g\; (\sum rs))$, we call the
|
|
494 |
%right hand side the "closed form" of $r\backslash s$.
|
|
495 |
%
|
|
496 |
%\begin{quote}\it
|
|
497 |
% Claim: For regular expressions $r_1 \cdot r_2$, we claim that
|
|
498 |
%\end{quote}
|
|
499 |
%\noindent
|
|
500 |
%We explain in detail how we reached those claims.
|
601
|
501 |
If we attempt to prove
|
|
502 |
\begin{center}
|
609
|
503 |
$\forall r. \; \exists N_r.\;\; s.t. \llbracket r\backslash_{rsimps} s \rrbracket_r \leq N_r$
|
601
|
504 |
\end{center}
|
|
505 |
using a naive induction on the structure of $r$,
|
|
506 |
then we are stuck at the inductive cases such as
|
|
507 |
$r_1\cdot r_2$.
|
|
508 |
The inductive hypotheses are:
|
|
509 |
\begin{center}
|
|
510 |
1: $\text{for } r_1, \text{there exists } N_{r_1}.\;\; s.t.
|
609
|
511 |
\;\;\forall s. \llbracket r_1 \backslash_{rsimps} s \rrbracket_r \leq N_{r_1}. $\\
|
601
|
512 |
2: $\text{for } r_2, \text{there exists } N_{r_2}.\;\; s.t.
|
609
|
513 |
\;\; \forall s. \llbracket r_2 \backslash_{rsimps} s \rrbracket_r \leq N_{r_2}. $
|
601
|
514 |
\end{center}
|
|
515 |
The inductive step to prove would be
|
|
516 |
\begin{center}
|
|
517 |
$\text{there exists } N_{r_1\cdot r_2}. \;\; s.t. \forall s.
|
609
|
518 |
\llbracket (r_1 \cdot r_2) \backslash_{rsimps} s \rrbracket_r \leq N_{r_1\cdot r_2}.$
|
601
|
519 |
\end{center}
|
|
520 |
The problem is that it is not clear what
|
609
|
521 |
$(r_1\cdot r_2) \backslash_{rsimps} s$ looks like,
|
601
|
522 |
and therefore $N_{r_1}$ and $N_{r_2}$ in the
|
|
523 |
inductive hypotheses cannot be directly used.
|
609
|
524 |
We have already seen that $(r_1 \cdot r_2)\backslash s$
|
|
525 |
and $(r^*)\backslash s$ can grow in a wild way.
|
613
|
526 |
|
609
|
527 |
The point is that they will be equivalent to a list of
|
|
528 |
terms $\sum rs$, where each term in $rs$ will
|
|
529 |
be made of $r_1 \backslash s' $, $r_2\backslash s'$,
|
|
530 |
and $r \backslash s'$ with $s' \in \textit{SubString} \; s$.
|
|
531 |
The list $\sum rs$ will then be de-duplicated by $\textit{rdistinct}$
|
|
532 |
in the simplification which saves $rs$ from growing indefinitely.
|
|
533 |
|
613
|
534 |
Based on this idea, we develop a proof in two steps.
|
|
535 |
First, we show the equality (where
|
609
|
536 |
$f$ and $g$ are functions that do not increase the size of the input)
|
|
537 |
\begin{center}
|
613
|
538 |
$r\backslash_{rsimps} s = f\; (\textit{rdistinct} \; (g\; \sum rs))$,
|
609
|
539 |
\end{center}
|
613
|
540 |
where $r = r_1 \cdot r_2$ or $r = r_0^*$ and so on.
|
|
541 |
For example, for $r_1 \cdot r_2$ we have the equality as
|
|
542 |
\begin{center}
|
|
543 |
$ \rderssimp{r_1 \cdot r_2}{s} =
|
|
544 |
\rsimp{(\sum (r_1 \backslash s \cdot r_2 ) \; :: \;(\map \; \rderssimp{r2}{\_} \;(\vsuf{s}{r_1})))}$
|
|
545 |
\end{center}
|
609
|
546 |
We call the right-hand-side the
|
|
547 |
\emph{Closed Form} of $(r_1 \cdot r_2)\backslash_{rsimps} s$.
|
613
|
548 |
Second, we will bound the closed form of r-regular expressions
|
|
549 |
using some estimation techniques
|
|
550 |
and then piece it together
|
|
551 |
with lemma \ref{sizeRelations} to show the bitcoded regular expressions
|
|
552 |
in our $\blexersimp$ are finitely bounded.
|
609
|
553 |
|
613
|
554 |
We will flesh out the first step of the proof we
|
|
555 |
sketched just now in the next section.
|
|
556 |
|
|
557 |
\section{Closed Forms}
|
609
|
558 |
In this section we introduce in detail
|
|
559 |
how the closed forms are obtained for regular expressions'
|
613
|
560 |
derivatives.
|
|
561 |
We start by proving some basic identities
|
609
|
562 |
involving the simplification functions for r-regular expressions.
|
613
|
563 |
After that we introduce the rewrite relations
|
|
564 |
$\rightsquigarrow_h$, $\rightsquigarrow^*_{scf}$
|
|
565 |
$\rightsquigarrow_f$ and $\rightsquigarrow_g$.
|
|
566 |
These relations involves similar techniques in chapter \ref{Bitcoded2}.
|
|
567 |
Finally, we use these identities to establish the
|
|
568 |
closed forms of the alternative regular expression,
|
|
569 |
the sequence regular expression, and the star regular expression.
|
609
|
570 |
%$r_1\cdot r_2$, $r^*$ and $\sum rs$.
|
601
|
571 |
|
|
572 |
|
609
|
573 |
|
613
|
574 |
\subsection{Some Basic Identities}
|
609
|
575 |
|
613
|
576 |
We now introduce lemmas
|
611
|
577 |
that are repeatedly used in later proofs.
|
|
578 |
Note that for the $\textit{rdistinct}$ function there
|
|
579 |
will be a lot of conversion from lists to sets.
|
613
|
580 |
We use $set$ to refere to the
|
611
|
581 |
function that converts a list $rs$ to the set
|
|
582 |
containing all the elements in $rs$.
|
|
583 |
\subsubsection{$\textit{rdistinct}$'s Does the Job of De-duplication}
|
543
|
584 |
The $\textit{rdistinct}$ function, as its name suggests, will
|
613
|
585 |
de-duplicate an r-regular expression list.
|
|
586 |
It will also remove any elements that
|
|
587 |
is already in the accumulator set.
|
555
|
588 |
\begin{lemma}\label{rdistinctDoesTheJob}
|
609
|
589 |
%The function $\textit{rdistinct}$ satisfies the following
|
|
590 |
%properties:
|
|
591 |
Assume we have the predicate $\textit{isDistinct}$\footnote{We omit its
|
|
592 |
recursive definition here, its Isabelle counterpart would be $\textit{distinct}$.}
|
|
593 |
readily defined
|
|
594 |
for testing
|
613
|
595 |
whether a list's elements are unique. Then the following
|
609
|
596 |
properties about $\textit{rdistinct}$ hold:
|
543
|
597 |
\begin{itemize}
|
|
598 |
\item
|
|
599 |
If $a \in acc$ then $a \notin (\rdistinct{rs}{acc})$.
|
|
600 |
\item
|
609
|
601 |
%If list $rs'$ is the result of $\rdistinct{rs}{acc}$,
|
|
602 |
$\textit{isDistinct} \;\;\; (\rdistinct{rs}{acc})$.
|
555
|
603 |
\item
|
609
|
604 |
$\textit{set} \; (\rdistinct{rs}{acc})
|
|
605 |
= (textit{set} \; rs) - acc$
|
543
|
606 |
\end{itemize}
|
|
607 |
\end{lemma}
|
555
|
608 |
\noindent
|
543
|
609 |
\begin{proof}
|
|
610 |
The first part is by an induction on $rs$.
|
555
|
611 |
The second and third part can be proven by using the
|
609
|
612 |
inductive cases of $\textit{rdistinct}$.
|
593
|
613 |
|
543
|
614 |
\end{proof}
|
|
615 |
|
|
616 |
\noindent
|
613
|
617 |
%$\textit{rdistinct}$ will out all regular expression terms
|
|
618 |
%that are in the accumulator, therefore
|
|
619 |
Concatenating a list $rs_a$ at the front of another
|
|
620 |
list $rs$ whose elements are all from the accumulator, and then calling $\textit{rdistinct}$
|
|
621 |
on the merged list, the output will be as if we had called $\textit{rdistinct}$
|
543
|
622 |
without the prepending of $rs$:
|
609
|
623 |
\begin{lemma}\label{rdistinctConcat}
|
554
|
624 |
The elements appearing in the accumulator will always be removed.
|
|
625 |
More precisely,
|
|
626 |
\begin{itemize}
|
|
627 |
\item
|
|
628 |
If $rs \subseteq rset$, then
|
|
629 |
$\rdistinct{rs@rsa }{acc} = \rdistinct{rsa }{acc}$.
|
|
630 |
\item
|
609
|
631 |
More generally, if $a \in rset$ and $\rdistinct{rs}{\{a\}} = []$,
|
554
|
632 |
then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{rs'}{rset}$
|
|
633 |
\end{itemize}
|
543
|
634 |
\end{lemma}
|
554
|
635 |
|
543
|
636 |
\begin{proof}
|
609
|
637 |
By induction on $rs$ and using \ref{rdistinctDoesTheJob}.
|
543
|
638 |
\end{proof}
|
|
639 |
\noindent
|
|
640 |
On the other hand, if an element $r$ does not appear in the input list waiting to be deduplicated,
|
|
641 |
then expanding the accumulator to include that element will not cause the output list to change:
|
611
|
642 |
\begin{lemma}\label{rdistinctOnDistinct}
|
543
|
643 |
The accumulator can be augmented to include elements not appearing in the input list,
|
|
644 |
and the output will not change.
|
|
645 |
\begin{itemize}
|
|
646 |
\item
|
611
|
647 |
If $r \notin rs$, then $\rdistinct{rs}{acc} = \rdistinct{rs}{(\{r\} \cup acc)}$.
|
543
|
648 |
\item
|
611
|
649 |
Particularly, if $\;\;\textit{isDistinct} \; rs$, then we have\\
|
543
|
650 |
\[ \rdistinct{rs}{\varnothing} = rs \]
|
|
651 |
\end{itemize}
|
|
652 |
\end{lemma}
|
|
653 |
\begin{proof}
|
|
654 |
The first half is by induction on $rs$. The second half is a corollary of the first.
|
|
655 |
\end{proof}
|
|
656 |
\noindent
|
611
|
657 |
The function $\textit{rdistinct}$ removes duplicates from anywhere in a list.
|
|
658 |
Despite being seemingly obvious,
|
|
659 |
the induction technique is not as straightforward.
|
554
|
660 |
\begin{lemma}\label{distinctRemovesMiddle}
|
|
661 |
The two properties hold if $r \in rs$:
|
|
662 |
\begin{itemize}
|
|
663 |
\item
|
555
|
664 |
$\rdistinct{rs}{rset} = \rdistinct{(rs @ [r])}{rset}$\\
|
|
665 |
and\\
|
554
|
666 |
$\rdistinct{(ab :: rs @ [ab])}{rset'} = \rdistinct{(ab :: rs)}{rset'}$
|
|
667 |
\item
|
555
|
668 |
$\rdistinct{ (rs @ rs') }{rset} = \rdistinct{rs @ [r] @ rs'}{rset}$\\
|
|
669 |
and\\
|
554
|
670 |
$\rdistinct{(ab :: rs @ [ab] @ rs'')}{rset'} =
|
593
|
671 |
\rdistinct{(ab :: rs @ rs'')}{rset'}$
|
554
|
672 |
\end{itemize}
|
|
673 |
\end{lemma}
|
|
674 |
\noindent
|
|
675 |
\begin{proof}
|
593
|
676 |
By induction on $rs$. All other variables are allowed to be arbitrary.
|
611
|
677 |
The second part of the lemma requires the first.
|
|
678 |
Note that for each part, the two sub-propositions need to be proven concurrently,
|
593
|
679 |
so that the induction goes through.
|
554
|
680 |
\end{proof}
|
555
|
681 |
\noindent
|
611
|
682 |
This allows us to prove a few more equivalence relations involving
|
|
683 |
$\textit{rdistinct}$ (it will be useful later):
|
555
|
684 |
\begin{lemma}\label{rdistinctConcatGeneral}
|
611
|
685 |
\mbox{}
|
555
|
686 |
\begin{itemize}
|
|
687 |
\item
|
|
688 |
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{((\rdistinct{rs}{\varnothing})@ rs')}{\varnothing}$
|
|
689 |
\item
|
|
690 |
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{(\rdistinct{rs}{\varnothing} @ rs')}{\varnothing}$
|
|
691 |
\item
|
|
692 |
If $rset' \subseteq rset$, then $\rdistinct{rs}{rset} =
|
|
693 |
\rdistinct{(\rdistinct{rs}{rset'})}{rset}$. As a corollary
|
|
694 |
of this,
|
|
695 |
\item
|
|
696 |
$\rdistinct{(rs @ rs')}{rset} = \rdistinct{
|
|
697 |
(\rdistinct{rs}{\varnothing}) @ rs')}{rset}$. This
|
|
698 |
gives another corollary use later:
|
|
699 |
\item
|
|
700 |
If $a \in rset$, then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{
|
|
701 |
(\rdistinct{(a :: rs)}{\varnothing} @ rs')}{rset} $,
|
|
702 |
|
|
703 |
\end{itemize}
|
|
704 |
\end{lemma}
|
|
705 |
\begin{proof}
|
|
706 |
By \ref{rdistinctDoesTheJob} and \ref{distinctRemovesMiddle}.
|
|
707 |
\end{proof}
|
611
|
708 |
\noindent
|
613
|
709 |
The next lemma is a more general form of \ref{rdistinctConcat},
|
|
710 |
it says that
|
611
|
711 |
$\textit{rdistinct}$ is composable w.r.t list concatenation:
|
|
712 |
\begin{lemma}\label{distinctRdistinctAppend}
|
|
713 |
If $\;\; \textit{isDistinct} \; rs_1$,
|
|
714 |
and $(set \; rs_1) \cap acc = \varnothing$,
|
|
715 |
then applying $\textit{rdistinct}$ on $rs_1 @ rs_a$ does not
|
|
716 |
have an effect on $rs_1$:
|
|
717 |
\[\textit{rdistinct}\; (rs_1 @ rsa)\;\, acc
|
|
718 |
= rs_1@(\textit{rdistinct} rsa \; (acc \cup rs_1))\]
|
|
719 |
\end{lemma}
|
|
720 |
\begin{proof}
|
|
721 |
By an induction on
|
|
722 |
$rs_1$, where $rsa$ and $acc$ are allowed to be arbitrary.
|
|
723 |
\end{proof}
|
|
724 |
\noindent
|
|
725 |
$\textit{rdistinct}$ needs to be applied only once, and
|
|
726 |
applying it multiple times does not cause any difference:
|
|
727 |
\begin{corollary}\label{distinctOnceEnough}
|
|
728 |
$\textit{rdistinct} \; (rs @ rsa) {} = \textit{rdistinct} \; (rdistinct \;
|
|
729 |
rs \{ \} @ (\textit{rdistinct} \; rs_a \; (set \; rs)))$
|
|
730 |
\end{corollary}
|
|
731 |
\begin{proof}
|
|
732 |
By lemma \ref{distinctRdistinctAppend}.
|
|
733 |
\end{proof}
|
555
|
734 |
|
611
|
735 |
\subsubsection{The Properties of $\textit{Rflts}$}
|
|
736 |
We give in this subsection some properties
|
|
737 |
involving $\backslash_r$, $\backslash_{rsimp}$, $\textit{rflts}$ and
|
|
738 |
$\textit{rsimp}_{ALTS} $, together with any non-trivial lemmas that lead to them.
|
543
|
739 |
These will be helpful in later closed form proofs, when
|
611
|
740 |
we want to transform derivative terms which have
|
|
741 |
%the ways in which multiple functions involving
|
|
742 |
%those are composed together
|
|
743 |
interleaving derivatives and simplifications applied to them.
|
543
|
744 |
|
611
|
745 |
\noindent
|
|
746 |
%When the function $\textit{Rflts}$
|
|
747 |
%is applied to the concatenation of two lists, the output can be calculated by first applying the
|
|
748 |
%functions on two lists separately, and then concatenating them together.
|
|
749 |
$\textit{Rflts}$ is composable in terms of concatenation:
|
554
|
750 |
\begin{lemma}\label{rfltsProps}
|
543
|
751 |
The function $\rflts$ has the below properties:\\
|
|
752 |
\begin{itemize}
|
|
753 |
\item
|
554
|
754 |
$\rflts \; (rs_1 @ rs_2) = \rflts \; rs_1 @ \rflts \; rs_2$
|
|
755 |
\item
|
|
756 |
If $r \neq \RZERO$ and $\nexists rs_1. r = \RALTS{rs}_1$, then $\rflts \; (r::rs) = r :: \rflts \; rs$
|
|
757 |
\item
|
|
758 |
$\rflts \; (rs @ [\RZERO]) = \rflts \; rs$
|
|
759 |
\item
|
|
760 |
$\rflts \; (rs' @ [\RALTS{rs}]) = \rflts \; rs'@rs$
|
|
761 |
\item
|
|
762 |
$\rflts \; (rs @ [\RONE]) = \rflts \; rs @ [\RONE]$
|
|
763 |
\item
|
|
764 |
If $r \neq \RZERO$ and $\nexists rs'. r = \RALTS{rs'}$ then $\rflts \; (rs @ [r])
|
|
765 |
= (\rflts \; rs) @ [r]$
|
555
|
766 |
\item
|
|
767 |
If $r = \RALTS{rs}$ and $r \in rs'$ then for all $r_1 \in rs.
|
|
768 |
r_1 \in \rflts \; rs'$.
|
|
769 |
\item
|
|
770 |
$\rflts \; (rs_a @ \RZERO :: rs_b) = \rflts \; (rs_a @ rs_b)$
|
543
|
771 |
\end{itemize}
|
|
772 |
\end{lemma}
|
|
773 |
\noindent
|
|
774 |
\begin{proof}
|
555
|
775 |
By induction on $rs_1$ in the first sub-lemma, and induction on $r$ in the second part,
|
|
776 |
and induction on $rs$, $rs'$, $rs$, $rs'$, $rs_a$ in the third, fourth, fifth, sixth and
|
|
777 |
last sub-lemma.
|
543
|
778 |
\end{proof}
|
611
|
779 |
\noindent
|
|
780 |
Now we introduce the property that the operations
|
|
781 |
derivative and $\rsimpalts$
|
|
782 |
commute, this will be used later in deriving the closed form for
|
|
783 |
the alternative regular expression:
|
|
784 |
\begin{lemma}\label{rderRsimpAltsCommute}
|
|
785 |
$\rder{x}{(\rsimpalts \; rs)} = \rsimpalts \; (\map \; (\rder{x}{\_}) \; rs)$
|
|
786 |
\end{lemma}
|
|
787 |
\noindent
|
614
|
788 |
|
|
789 |
\subsubsection{The $RL$ Function: Language Interpretation of $\textit{Rrexp}$s}
|
|
790 |
Much like the definition of $L$ on plain regular expressions, one could also
|
|
791 |
define the language interpretation of $\rrexp$s.
|
|
792 |
\begin{center}
|
|
793 |
\begin{tabular}{lcl}
|
|
794 |
$RL \; (\ZERO)$ & $\dn$ & $\phi$\\
|
|
795 |
$RL \; (\ONE)$ & $\dn$ & $\{[]\}$\\
|
|
796 |
$RL \; (c)$ & $\dn$ & $\{[c]\}$\\
|
|
797 |
$RL \; \sum rs$ & $\dn$ & $ \bigcup_{r \in rs} (RL \; r)$\\
|
|
798 |
$RL \; (r_1 \cdot r_2)$ & $\dn$ & $ RL \; (r_1) @ RL \; (r_2)$\\
|
|
799 |
$RL \; (r^*)$ & $\dn$ & $ (RL(r))^*$
|
|
800 |
\end{tabular}
|
|
801 |
\end{center}
|
|
802 |
\noindent
|
|
803 |
The main use of $RL$ is to establish some connections between $\rsimp{}$
|
|
804 |
and $\rnullable{}$:
|
|
805 |
\begin{lemma}
|
|
806 |
The following properties hold:
|
|
807 |
\begin{itemize}
|
|
808 |
\item
|
|
809 |
If $\rnullable{r}$, then $\rsimp{r} \neq \RZERO$.
|
|
810 |
\item
|
|
811 |
$\rnullable{r \backslash s} \quad $ if and only if $\quad \rnullable{\rderssimp{r}{s}}$.
|
|
812 |
\end{itemize}
|
|
813 |
\end{lemma}
|
|
814 |
\begin{proof}
|
|
815 |
The first part is by induction on $r$.
|
|
816 |
The second part is true because property
|
|
817 |
\[ RL \; r = RL \; (\rsimp{r})\] holds.
|
|
818 |
\end{proof}
|
|
819 |
|
|
820 |
\subsubsection{Simplified $\textit{Rrexp}$s are Good}
|
|
821 |
We formalise the notion of ``good" regular expressions,
|
|
822 |
which means regular expressions that
|
|
823 |
are not fully simplified. For alternative regular expressions that means they
|
|
824 |
do not contain any nested alternatives like
|
|
825 |
\[ r_1 + (r_2 + r_3) \], un-removed $\RZERO$s like \[\RZERO + r\]
|
|
826 |
or duplicate elements in a children regular expression list like \[ \sum [r, r, \ldots]\]:
|
|
827 |
\begin{center}
|
|
828 |
\begin{tabular}{@{}lcl@{}}
|
|
829 |
$\good\; \RZERO$ & $\dn$ & $\textit{false}$\\
|
|
830 |
$\good\; \RONE$ & $\dn$ & $\textit{true}$\\
|
|
831 |
$\good\; \RCHAR{c}$ & $\dn$ & $\btrue$\\
|
|
832 |
$\good\; \RALTS{[]}$ & $\dn$ & $\bfalse$\\
|
|
833 |
$\good\; \RALTS{[r]}$ & $\dn$ & $\bfalse$\\
|
|
834 |
$\good\; \RALTS{r_1 :: r_2 :: rs}$ & $\dn$ &
|
|
835 |
$\textit{isDistinct} \; (r_1 :: r_2 :: rs) \;$\\
|
|
836 |
& & $\textit{and}\; (\forall r' \in (r_1 :: r_2 :: rs).\; \good \; r'\; \, \textit{and}\; \, \textit{nonAlt}\; r')$\\
|
|
837 |
$\good \; \RSEQ{\RZERO}{r}$ & $\dn$ & $\bfalse$\\
|
|
838 |
$\good \; \RSEQ{\RONE}{r}$ & $\dn$ & $\bfalse$\\
|
|
839 |
$\good \; \RSEQ{r}{\RZERO}$ & $\dn$ & $\bfalse$\\
|
|
840 |
$\good \; \RSEQ{r_1}{r_2}$ & $\dn$ & $\good \; r_1 \;\, \textit{and} \;\, \good \; r_2$\\
|
|
841 |
$\good \; \RSTAR{r}$ & $\dn$ & $\btrue$\\
|
|
842 |
\end{tabular}
|
|
843 |
\end{center}
|
|
844 |
\noindent
|
|
845 |
The predicate $\textit{nonAlt}$ evaluates to true when the regular expression is not an
|
|
846 |
alternative, and false otherwise.
|
|
847 |
The $\good$ property is preserved under $\rsimp_{ALTS}$, provided that
|
|
848 |
its non-empty argument list of expressions are all good themsleves, and $\textit{nonAlt}$,
|
|
849 |
and unique:
|
|
850 |
\begin{lemma}\label{rsimpaltsGood}
|
|
851 |
If $rs \neq []$ and forall $r \in rs. \textit{nonAlt} \; r$ and $\textit{isDistinct} \; rs$,
|
|
852 |
then $\good \; (\rsimpalts \; rs)$ if and only if forall $r \in rs. \; \good \; r$.
|
|
853 |
\end{lemma}
|
|
854 |
\noindent
|
|
855 |
We also note that
|
|
856 |
if a regular expression $r$ is good, then $\rflts$ on the singleton
|
|
857 |
list $[r]$ will not break goodness:
|
|
858 |
\begin{lemma}\label{flts2}
|
|
859 |
If $\good \; r$, then forall $r' \in \rflts \; [r]. \; \good \; r'$ and $\textit{nonAlt} \; r'$.
|
|
860 |
\end{lemma}
|
|
861 |
\begin{proof}
|
|
862 |
By an induction on $r$.
|
|
863 |
\end{proof}
|
|
864 |
\noindent
|
|
865 |
The other observation we make about $\rsimp{r}$ is that it never
|
|
866 |
comes with nested alternatives, which we describe as the $\nonnested$
|
|
867 |
property:
|
|
868 |
\begin{center}
|
|
869 |
\begin{tabular}{lcl}
|
|
870 |
$\nonnested \; \, \sum []$ & $\dn$ & $\btrue$\\
|
|
871 |
$\nonnested \; \, \sum ((\sum rs_1) :: rs_2)$ & $\dn$ & $\bfalse$\\
|
|
872 |
$\nonnested \; \, \sum (r :: rs)$ & $\dn$ & $\nonnested (\sum rs)$\\
|
|
873 |
$\nonnested \; \, r $ & $\dn$ & $\btrue$
|
|
874 |
\end{tabular}
|
|
875 |
\end{center}
|
|
876 |
\noindent
|
|
877 |
The $\rflts$ function
|
|
878 |
always opens up nested alternatives,
|
|
879 |
which enables $\rsimp$ to be non-nested:
|
|
880 |
|
|
881 |
\begin{lemma}\label{nonnestedRsimp}
|
|
882 |
$\nonnested \; (\rsimp{r})$
|
|
883 |
\end{lemma}
|
|
884 |
\begin{proof}
|
|
885 |
By an induction on $r$.
|
|
886 |
\end{proof}
|
|
887 |
\noindent
|
|
888 |
With this we could prove that a regular expressions
|
|
889 |
after simplification and flattening and de-duplication,
|
|
890 |
will not contain any alternative regular expression directly:
|
|
891 |
\begin{lemma}\label{nonaltFltsRd}
|
|
892 |
If $x \in \rdistinct{\rflts\; (\map \; \rsimp{} \; rs)}{\varnothing}$
|
|
893 |
then $\textit{nonAlt} \; x$.
|
|
894 |
\end{lemma}
|
|
895 |
\begin{proof}
|
|
896 |
By \ref{nonnestedRsimp}.
|
|
897 |
\end{proof}
|
|
898 |
\noindent
|
|
899 |
The other thing we know is that once $\rsimp{}$ had finished
|
|
900 |
processing an alternative regular expression, it will not
|
|
901 |
contain any $\RZERO$s, this is because all the recursive
|
|
902 |
calls to the simplification on the children regular expressions
|
|
903 |
make the children good, and $\rflts$ will not take out
|
|
904 |
any $\RZERO$s out of a good regular expression list,
|
|
905 |
and $\rdistinct{}$ will not mess with the result.
|
|
906 |
\begin{lemma}\label{flts3Obv}
|
|
907 |
The following are true:
|
|
908 |
\begin{itemize}
|
|
909 |
\item
|
|
910 |
If for all $r \in rs. \, \good \; r $ or $r = \RZERO$,
|
|
911 |
then for all $r \in \rflts\; rs. \, \good \; r$.
|
|
912 |
\item
|
|
913 |
If $x \in \rdistinct{\rflts\; (\map \; rsimp{}\; rs)}{\varnothing}$
|
|
914 |
and for all $y$ such that $\llbracket y \rrbracket_r$ less than
|
|
915 |
$\llbracket rs \rrbracket_r + 1$, either
|
|
916 |
$\good \; (\rsimp{y})$ or $\rsimp{y} = \RZERO$,
|
|
917 |
then $\good \; x$.
|
|
918 |
\end{itemize}
|
|
919 |
\end{lemma}
|
|
920 |
\begin{proof}
|
|
921 |
The first part is by induction on $rs$, where the induction
|
|
922 |
rule is the inductive cases for $\rflts$.
|
|
923 |
The second part is a corollary from the first part.
|
|
924 |
\end{proof}
|
|
925 |
|
|
926 |
And this leads to good structural property of $\rsimp{}$,
|
|
927 |
that after simplification, a regular expression is
|
|
928 |
either good or $\RZERO$:
|
|
929 |
\begin{lemma}\label{good1}
|
|
930 |
For any r-regular expression $r$, $\good \; \rsimp{r}$ or $\rsimp{r} = \RZERO$.
|
|
931 |
\end{lemma}
|
|
932 |
\begin{proof}
|
|
933 |
By an induction on $r$. The inductive measure is the size $\llbracket \rrbracket_r$.
|
|
934 |
Lemma \ref{rsimpSize} says that
|
|
935 |
$\llbracket \rsimp{r}\rrbracket_r$ is smaller than or equal to
|
|
936 |
$\llbracket r \rrbracket_r$.
|
|
937 |
Therefore, in the $r_1 \cdot r_2$ and $\sum rs$ case,
|
|
938 |
Inductive hypothesis applies to the children regular expressions
|
|
939 |
$r_1$, $r_2$, etc. The lemma \ref{flts3Obv}'s precondition is satisfied
|
|
940 |
by that as well.
|
|
941 |
The lemmas \ref{nonnestedRsimp} and \ref{nonaltFltsRd} are used
|
|
942 |
to ensure that goodness is preserved at the topmost level.
|
|
943 |
\end{proof}
|
|
944 |
We shall prove that any good regular expression is
|
|
945 |
a fixed-point for $\rsimp{}$.
|
|
946 |
First we prove an auxiliary lemma:
|
|
947 |
\begin{lemma}\label{goodaltsNonalt}
|
|
948 |
If $\good \; \sum rs$, then $\rflts\; rs = rs$.
|
|
949 |
\end{lemma}
|
|
950 |
\begin{proof}
|
|
951 |
By an induction on $\sum rs$. The inductive rules are the cases
|
|
952 |
for $\good$.
|
|
953 |
\end{proof}
|
|
954 |
\noindent
|
|
955 |
Now we are ready to prove that good regular expressions are invariant
|
|
956 |
of $\rsimp{}$ application:
|
|
957 |
\begin{lemma}\label{test}
|
|
958 |
If $\good \;r$ then $\rsimp{r} = r$.
|
|
959 |
\end{lemma}
|
|
960 |
\begin{proof}
|
|
961 |
By an induction on the inductive cases of $\good$, using lemmas
|
|
962 |
\ref{goodaltsNonalt} and \ref{rdistinctOnDistinct}.
|
|
963 |
The lemma \ref{goodaltsNonalt} is used in the alternative
|
|
964 |
case where 2 or more elements are present in the list.
|
|
965 |
\end{proof}
|
|
966 |
\noindent
|
|
967 |
Given below is a property involving $\rflts$, $\rdistinct{}{}$, $\rsimp{}$ and $\rsimp_{ALTS}$,
|
|
968 |
which requires $\ref{good1}$ to go through smoothly.
|
|
969 |
It says that an application of $\rsimp_{ALTS}$ can be "absorbed",
|
|
970 |
if it its output is concatenated with a list and then applied to $\rflts$.
|
|
971 |
\begin{lemma}\label{flattenRsimpalts}
|
|
972 |
$\rflts \; ( (\rsimp_{ALTS} \;
|
|
973 |
(\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing})) ::
|
|
974 |
\map \; \rsimp{} \; rs' ) =
|
|
975 |
\rflts \; ( (\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing}) @ (
|
|
976 |
\map \; \rsimp{rs'}))$
|
|
977 |
|
|
978 |
|
|
979 |
\end{lemma}
|
|
980 |
\begin{proof}
|
|
981 |
By \ref{good1}.
|
|
982 |
\end{proof}
|
|
983 |
\noindent
|
|
984 |
|
|
985 |
|
|
986 |
|
|
987 |
|
|
988 |
|
|
989 |
We are also ready to prove that $\textit{rsimp}$ is idempotent.
|
|
990 |
\subsubsection{$\rsimp$ is Idempotent}
|
|
991 |
The idempotency of $\rsimp$ is very useful in
|
|
992 |
manipulating regular expression terms into desired
|
|
993 |
forms so that key steps allowing further rewriting to closed forms
|
|
994 |
are possible.
|
|
995 |
\begin{lemma}\label{rsimpIdem}
|
|
996 |
$\rsimp{r} = \rsimp{\rsimp{r}}$
|
|
997 |
\end{lemma}
|
|
998 |
|
|
999 |
\begin{proof}
|
|
1000 |
By \ref{test} and \ref{good1}.
|
|
1001 |
\end{proof}
|
|
1002 |
\noindent
|
|
1003 |
This property means we do not have to repeatedly
|
|
1004 |
apply simplification in each step, which justifies
|
|
1005 |
our definition of $\blexersimp$.
|
|
1006 |
|
|
1007 |
|
|
1008 |
On the other hand, we could repeat the same $\rsimp{}$ applications
|
|
1009 |
on regular expressions as many times as we want, if we have at least
|
|
1010 |
one simplification applied to it, and apply it wherever we would like to:
|
|
1011 |
\begin{corollary}\label{headOneMoreSimp}
|
|
1012 |
The following properties hold, directly from \ref{rsimpIdem}:
|
|
1013 |
|
|
1014 |
\begin{itemize}
|
|
1015 |
\item
|
|
1016 |
$\map \; \rsimp{(r :: rs)} = \map \; \rsimp{} \; (\rsimp{r} :: rs)$
|
|
1017 |
\item
|
|
1018 |
$\rsimp{(\RALTS{rs})} = \rsimp{(\RALTS{\map \; \rsimp{} \; rs})}$
|
|
1019 |
\end{itemize}
|
|
1020 |
\end{corollary}
|
|
1021 |
\noindent
|
|
1022 |
This will be useful in later closed form proof's rewriting steps.
|
|
1023 |
Similarly, we point out the following useful facts below:
|
|
1024 |
\begin{lemma}
|
|
1025 |
The following equalities hold if $r = \rsimp{r'}$ for some $r'$:
|
|
1026 |
\begin{itemize}
|
|
1027 |
\item
|
|
1028 |
If $r = \sum rs$ then $\rsimpalts \; rs = \sum rs$.
|
|
1029 |
\item
|
|
1030 |
If $r = \sum rs$ then $\rdistinct{rs}{\varnothing} = rs$.
|
|
1031 |
\item
|
|
1032 |
$\rsimpalts \; (\rdistinct{\rflts \; [r]}{\varnothing}) = r$.
|
|
1033 |
\end{itemize}
|
|
1034 |
\end{lemma}
|
|
1035 |
\begin{proof}
|
|
1036 |
By application of lemmas \ref{rsimpIdem} and \ref{good1}.
|
|
1037 |
\end{proof}
|
|
1038 |
|
|
1039 |
\noindent
|
|
1040 |
With the idempotency of $\rsimp{}$ and its corollaries,
|
|
1041 |
we can start proving some key equalities leading to the
|
|
1042 |
closed forms.
|
|
1043 |
Now presented are a few equivalent terms under $\rsimp{}$.
|
|
1044 |
We use $r_1 \sequal r_2 $ here to denote $\rsimp{r_1} = \rsimp{r_2}$.
|
|
1045 |
\begin{lemma}
|
|
1046 |
\begin{itemize}
|
|
1047 |
The following equivalence hold:
|
|
1048 |
\item
|
|
1049 |
$\rsimpalts \; (\RZERO :: rs) \sequal \rsimpalts\; rs$
|
|
1050 |
\item
|
|
1051 |
$\rsimpalts \; rs \sequal \rsimpalts (\map \; \rsimp{} \; rs)$
|
|
1052 |
\item
|
|
1053 |
$\RALTS{\RALTS{rs}} \sequal \RALTS{rs}$
|
|
1054 |
\item
|
|
1055 |
$\sum ((\sum rs_a) :: rs_b) \sequal \sum rs_a @ rs_b$
|
|
1056 |
\item
|
|
1057 |
$\RALTS{rs} = \RALTS{\map \; \rsimp{} \; rs}$
|
|
1058 |
\end{itemize}
|
|
1059 |
\end{lemma}
|
|
1060 |
\begin{proof}
|
|
1061 |
By induction on the lists involved.
|
|
1062 |
\end{proof}
|
|
1063 |
\noindent
|
|
1064 |
The above allows us to prove
|
|
1065 |
two similar equalities (which are a bit more involved).
|
|
1066 |
It says that we could flatten out the elements
|
|
1067 |
before simplification and still get the same result.
|
|
1068 |
\begin{lemma}\label{simpFlatten3}
|
|
1069 |
One can flatten the inside $\sum$ of a $\sum$ if it is being
|
|
1070 |
simplified. Concretely,
|
|
1071 |
\begin{itemize}
|
|
1072 |
\item
|
|
1073 |
If for all $r \in rs, rs', rs''$, we have $\good \; r $
|
|
1074 |
or $r = \RZERO$, then $\sum (rs' @ rs @ rs'') \sequal
|
|
1075 |
\sum (rs' @ [\sum rs] @ rs'')$ holds. As a corollary,
|
|
1076 |
\item
|
|
1077 |
$\sum (rs' @ [\sum rs] @ rs'') \sequal \sum (rs' @ rs @ rs'')$
|
|
1078 |
\end{itemize}
|
|
1079 |
\end{lemma}
|
|
1080 |
\begin{proof}
|
|
1081 |
By rewriting steps involving the use of \ref{test} and \ref{rdistinctConcatGeneral}.
|
|
1082 |
The second sub-lemma is a corollary of the previous.
|
|
1083 |
\end{proof}
|
|
1084 |
%Rewriting steps not put in--too long and complicated-------------------------------
|
|
1085 |
\begin{comment}
|
|
1086 |
\begin{center}
|
|
1087 |
$\rsimp{\sum (rs' @ rs @ rs'')} \stackrel{def of bsimp}{=}$ \\
|
|
1088 |
$\rsimpalts \; (\rdistinct{\rflts \; ((\map \; \rsimp{}\; rs') @ (\map \; \rsimp{} \; rs ) @ (\map \; \rsimp{} \; rs''))}{\varnothing})$ \\
|
|
1089 |
$\stackrel{by \ref{test}}{=}
|
|
1090 |
\rsimpalts \; (\rdistinct{(\rflts \; rs' @ \rflts \; rs @ \rflts \; rs'')}{
|
|
1091 |
\varnothing})$\\
|
|
1092 |
$\stackrel{by \ref{rdistinctConcatGeneral}}{=}
|
|
1093 |
\rsimpalts \; (\rdistinct{\rflts \; rs'}{\varnothing} @ \rdistinct{(
|
|
1094 |
\rflts\; rs @ \rflts \; rs'')}{\rflts \; rs'})$\\
|
|
1095 |
|
|
1096 |
\end{center}
|
|
1097 |
\end{comment}
|
|
1098 |
%Rewriting steps not put in--too long and complicated-------------------------------
|
|
1099 |
\noindent
|
|
1100 |
|
|
1101 |
|
613
|
1102 |
We need more equalities like the above to enable a closed form,
|
|
1103 |
for which we need to introduce a few rewrite relations
|
|
1104 |
to help
|
|
1105 |
us obtain them.
|
554
|
1106 |
|
610
|
1107 |
\subsection{The rewrite relation $\hrewrite$ , $\scfrewrites$ , $\frewrite$ and $\grewrite$}
|
613
|
1108 |
Inspired by the success we had in the correctness proof
|
|
1109 |
in \ref{Bitcoded2},
|
|
1110 |
we follow suit here, defining atomic simplification
|
|
1111 |
steps as ``small-step'' rewriting steps. This allows capturing
|
555
|
1112 |
similarities between terms that would be otherwise
|
|
1113 |
hard to express.
|
|
1114 |
|
557
|
1115 |
We use $\hrewrite$ for one-step atomic rewrite of
|
|
1116 |
regular expression simplification,
|
555
|
1117 |
$\frewrite$ for rewrite of list of regular expressions that
|
|
1118 |
include all operations carried out in $\rflts$, and $\grewrite$ for
|
613
|
1119 |
rewriting a list of regular expressions possible in both $\rflts$ and $\textit{rdistinct}$.
|
555
|
1120 |
Their reflexive transitive closures are used to denote zero or many steps,
|
|
1121 |
as was the case in the previous chapter.
|
613
|
1122 |
As we have already
|
|
1123 |
done something similar, the presentation about
|
|
1124 |
these rewriting rules will be more concise than that in \ref{Bitcoded2}.
|
554
|
1125 |
To differentiate between the rewriting steps for annotated regular expressions
|
|
1126 |
and $\rrexp$s, we add characters $h$ and $g$ below the squig arrow symbol
|
|
1127 |
to mean atomic simplification transitions
|
|
1128 |
of $\rrexp$s and $\rrexp$ lists, respectively.
|
|
1129 |
|
555
|
1130 |
|
|
1131 |
|
|
1132 |
|
613
|
1133 |
\begin{figure}[H]
|
554
|
1134 |
\begin{center}
|
593
|
1135 |
\begin{mathpar}
|
|
1136 |
\inferrule[RSEQ0L]{}{\RZERO \cdot r_2 \hrewrite \RZERO\\}
|
555
|
1137 |
|
593
|
1138 |
\inferrule[RSEQ0R]{}{r_1 \cdot \RZERO \hrewrite \RZERO\\}
|
555
|
1139 |
|
593
|
1140 |
\inferrule[RSEQ1]{}{(\RONE \cdot r) \hrewrite r\\}\\
|
555
|
1141 |
|
593
|
1142 |
\inferrule[RSEQL]{ r_1 \hrewrite r_2}{r_1 \cdot r_3 \hrewrite r_2 \cdot r_3\\}
|
|
1143 |
|
|
1144 |
\inferrule[RSEQR]{ r_3 \hrewrite r_4}{r_1 \cdot r_3 \hrewrite r_1 \cdot r_4\\}\\
|
555
|
1145 |
|
593
|
1146 |
\inferrule[RALTSChild]{r \hrewrite r'}{\sum (rs_1 @ [r] @ rs_2) \hrewrite \sum (rs_1 @ [r'] @ rs_2)\\}
|
555
|
1147 |
|
593
|
1148 |
\inferrule[RALTS0]{}{\sum (rs_a @ [\RZERO] @ rs_b) \hrewrite \sum (rs_a @ rs_b)}
|
555
|
1149 |
|
593
|
1150 |
\inferrule[RALTSNested]{}{\sum (rs_a @ [\sum rs_1] @ rs_b) \hrewrite \sum (rs_a @ rs_1 @ rs_b)}
|
555
|
1151 |
|
593
|
1152 |
\inferrule[RALTSNil]{}{ \sum [] \hrewrite \RZERO\\}
|
555
|
1153 |
|
593
|
1154 |
\inferrule[RALTSSingle]{}{ \sum [r] \hrewrite r\\}
|
555
|
1155 |
|
593
|
1156 |
\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}
|
555
|
1157 |
|
593
|
1158 |
\end{mathpar}
|
555
|
1159 |
\end{center}
|
613
|
1160 |
\caption{List of one-step rewrite rules for r-regular expressions ($\hrewrite$)}\label{hRewrite}
|
|
1161 |
\end{figure}
|
554
|
1162 |
|
557
|
1163 |
|
613
|
1164 |
Like $\rightsquigarrow_s$, it is
|
|
1165 |
convenient to define rewrite rules for a list of regular expressions,
|
593
|
1166 |
where each element can rewrite in many steps to the other (scf stands for
|
|
1167 |
li\emph{s}t \emph{c}losed \emph{f}orm). This relation is similar to the
|
|
1168 |
$\stackrel{s*}{\rightsquigarrow}$ for annotated regular expressions.
|
557
|
1169 |
|
613
|
1170 |
\begin{figure}[H]
|
557
|
1171 |
\begin{center}
|
593
|
1172 |
\begin{mathpar}
|
|
1173 |
\inferrule{}{[] \scfrewrites [] }
|
613
|
1174 |
|
593
|
1175 |
\inferrule{r \hrewrites r' \\ rs \scfrewrites rs'}{r :: rs \scfrewrites r' :: rs'}
|
|
1176 |
\end{mathpar}
|
557
|
1177 |
\end{center}
|
613
|
1178 |
\caption{List of one-step rewrite rules for a list of r-regular expressions}\label{scfRewrite}
|
|
1179 |
\end{figure}
|
555
|
1180 |
%frewrite
|
593
|
1181 |
List of one-step rewrite rules for flattening
|
|
1182 |
a list of regular expressions($\frewrite$):
|
613
|
1183 |
\begin{figure}[H]
|
555
|
1184 |
\begin{center}
|
593
|
1185 |
\begin{mathpar}
|
|
1186 |
\inferrule{}{\RZERO :: rs \frewrite rs \\}
|
555
|
1187 |
|
593
|
1188 |
\inferrule{}{(\sum rs) :: rs_a \frewrite rs @ rs_a \\}
|
555
|
1189 |
|
593
|
1190 |
\inferrule{rs_1 \frewrite rs_2}{r :: rs_1 \frewrite r :: rs_2}
|
|
1191 |
\end{mathpar}
|
555
|
1192 |
\end{center}
|
613
|
1193 |
\caption{List of one-step rewrite rules characterising the $\rflts$ operation on a list}\label{fRewrites}
|
|
1194 |
\end{figure}
|
555
|
1195 |
|
593
|
1196 |
Lists of one-step rewrite rules for flattening and de-duplicating
|
|
1197 |
a list of regular expressions ($\grewrite$):
|
613
|
1198 |
\begin{figure}[H]
|
555
|
1199 |
\begin{center}
|
593
|
1200 |
\begin{mathpar}
|
|
1201 |
\inferrule{}{\RZERO :: rs \grewrite rs \\}
|
532
|
1202 |
|
593
|
1203 |
\inferrule{}{(\sum rs) :: rs_a \grewrite rs @ rs_a \\}
|
555
|
1204 |
|
593
|
1205 |
\inferrule{rs_1 \grewrite rs_2}{r :: rs_1 \grewrite r :: rs_2}
|
555
|
1206 |
|
593
|
1207 |
\inferrule[dB]{}{rs_a @ [a] @ rs_b @[a] @ rs_c \grewrite rs_a @ [a] @ rsb @ rsc}
|
|
1208 |
\end{mathpar}
|
555
|
1209 |
\end{center}
|
613
|
1210 |
\caption{List of one-step rewrite rules characterising the $\rflts$ and $\textit{rdistinct}$
|
|
1211 |
operations}\label{gRewrite}
|
|
1212 |
\end{figure}
|
555
|
1213 |
\noindent
|
611
|
1214 |
We defined
|
613
|
1215 |
two separate list rewriting relations $\frewrite$ and $\grewrite$.
|
611
|
1216 |
The rewriting steps that take place during
|
|
1217 |
flattening is characterised by $\frewrite$.
|
|
1218 |
$\grewrite$ characterises both flattening and de-duplicating.
|
557
|
1219 |
Sometimes $\grewrites$ is slightly too powerful
|
613
|
1220 |
so we would rather use $\frewrites$ to prove
|
|
1221 |
%because we only
|
|
1222 |
equalities related to $\rflts$.
|
|
1223 |
%certain equivalence under the rewriting steps of $\frewrites$.
|
556
|
1224 |
For example, when proving the closed-form for the alternative regular expression,
|
613
|
1225 |
one of the equalities needed is:
|
|
1226 |
\begin{center}
|
557
|
1227 |
$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
|
593
|
1228 |
\sum (\rDistinct \;\; (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
|
|
1229 |
$
|
613
|
1230 |
\end{center}
|
556
|
1231 |
\noindent
|
|
1232 |
Proving this is by first showing
|
557
|
1233 |
\begin{lemma}\label{earlyLaterDerFrewrites}
|
556
|
1234 |
$\map \; (\_ \backslash x) \; (\rflts \; rs) \frewrites
|
557
|
1235 |
\rflts \; (\map \; (\_ \backslash x) \; rs)$
|
556
|
1236 |
\end{lemma}
|
|
1237 |
\noindent
|
613
|
1238 |
and then the equivalence between two terms
|
|
1239 |
that can reduce in many steps to each other.
|
556
|
1240 |
\begin{lemma}\label{frewritesSimpeq}
|
|
1241 |
If $rs_1 \frewrites rs_2 $, then $\sum (\rDistinct \; rs_1 \; \varnothing) \sequal
|
557
|
1242 |
\sum (\rDistinct \; rs_2 \; \varnothing)$.
|
556
|
1243 |
\end{lemma}
|
557
|
1244 |
\noindent
|
613
|
1245 |
|
|
1246 |
\begin{corollary}
|
|
1247 |
$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
|
|
1248 |
\sum (\rDistinct \;\; (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
|
|
1249 |
$
|
|
1250 |
\end{corollary}
|
|
1251 |
|
557
|
1252 |
But this trick will not work for $\grewrites$.
|
|
1253 |
For example, a rewriting step in proving
|
|
1254 |
closed forms is:
|
|
1255 |
\begin{center}
|
593
|
1256 |
$\rsimp{(\rsimpalts \; (\map \; (\_ \backslash x) \; (\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs}))))}{\varnothing})))}$\\
|
|
1257 |
$=$ \\
|
|
1258 |
$\rsimp{(\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \; (\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}))} $
|
|
1259 |
\noindent
|
557
|
1260 |
\end{center}
|
|
1261 |
For this one would hope to have a rewriting relation between the two lists involved,
|
|
1262 |
similar to \ref{earlyLaterDerFrewrites}. However, it turns out that
|
556
|
1263 |
\begin{center}
|
593
|
1264 |
$\map \; (\_ \backslash x) \; (\rDistinct \; rs \; rset) \grewrites \rDistinct \; (\map \;
|
|
1265 |
(\_ \backslash x) \; rs) \; ( rset \backslash x)$
|
556
|
1266 |
\end{center}
|
|
1267 |
\noindent
|
557
|
1268 |
does $\mathbf{not}$ hold in general.
|
|
1269 |
For this rewriting step we will introduce some slightly more cumbersome
|
|
1270 |
proof technique in later sections.
|
|
1271 |
The point is that $\frewrite$
|
613
|
1272 |
allows us to prove equivalence in a straightforward way that is
|
|
1273 |
not possible for $\grewrite$.
|
555
|
1274 |
|
556
|
1275 |
|
557
|
1276 |
\subsubsection{Terms That Can Be Rewritten Using $\hrewrites$, $\grewrites$, and $\frewrites$}
|
613
|
1277 |
In this part, we present lemmas stating
|
|
1278 |
pairs of r-regular expressions or r-regular expression lists
|
|
1279 |
where one could rewrite from one in many steps to the other.
|
|
1280 |
Most of the proofs to these lemmas are straightforward, using
|
|
1281 |
an induction on the inductive cases of the corresponding rewriting relations.
|
|
1282 |
These proofs will therefore be omitted when this is the case.
|
557
|
1283 |
We present in the below lemma a few pairs of terms that are rewritable via
|
|
1284 |
$\grewrites$:
|
|
1285 |
\begin{lemma}\label{gstarRdistinctGeneral}
|
613
|
1286 |
\mbox{}
|
557
|
1287 |
\begin{itemize}
|
|
1288 |
\item
|
|
1289 |
$rs_1 @ rs \grewrites rs_1 @ (\rDistinct \; rs \; rs_1)$
|
|
1290 |
\item
|
|
1291 |
$rs \grewrites \rDistinct \; rs \; \varnothing$
|
|
1292 |
\item
|
|
1293 |
$rs_a @ (\rDistinct \; rs \; rs_a) \grewrites rs_a @ (\rDistinct \;
|
|
1294 |
rs \; (\{\RZERO\} \cup rs_a))$
|
|
1295 |
\item
|
|
1296 |
$rs \;\; @ \;\; \rDistinct \; rs_a \; rset \grewrites rs @ \rDistinct \; rs_a \;
|
|
1297 |
(rest \cup rs)$
|
|
1298 |
|
|
1299 |
\end{itemize}
|
|
1300 |
\end{lemma}
|
|
1301 |
\noindent
|
|
1302 |
If a pair of terms $rs_1, rs_2$ are rewritable via $\grewrites$ to each other,
|
|
1303 |
then they are equivalent under $\rsimp{}$:
|
|
1304 |
\begin{lemma}\label{grewritesSimpalts}
|
|
1305 |
If $rs_1 \grewrites rs_2$, then
|
613
|
1306 |
we have the following equivalence:
|
557
|
1307 |
\begin{itemize}
|
|
1308 |
\item
|
|
1309 |
$\sum rs_1 \sequal \sum rs_2$
|
|
1310 |
\item
|
|
1311 |
$\rsimpalts \; rs_1 \sequal \rsimpalts \; rs_2$
|
|
1312 |
\end{itemize}
|
|
1313 |
\end{lemma}
|
|
1314 |
\noindent
|
|
1315 |
Here are a few connecting lemmas showing that
|
|
1316 |
if a list of regular expressions can be rewritten using $\grewrites$ or $\frewrites $ or
|
|
1317 |
$\scfrewrites$,
|
|
1318 |
then an alternative constructor taking the list can also be rewritten using $\hrewrites$:
|
|
1319 |
\begin{lemma}
|
|
1320 |
\begin{itemize}
|
|
1321 |
\item
|
|
1322 |
If $rs \grewrites rs'$ then $\sum rs \hrewrites \sum rs'$.
|
|
1323 |
\item
|
|
1324 |
If $rs \grewrites rs'$ then $\sum rs \hrewrites \rsimpalts \; rs'$
|
|
1325 |
\item
|
|
1326 |
If $rs_1 \scfrewrites rs_2$ then $\sum (rs @ rs_1) \hrewrites \sum (rs @ rs_2)$
|
|
1327 |
\item
|
|
1328 |
If $rs_1 \scfrewrites rs_2$ then $\sum rs_1 \hrewrites \sum rs_2$
|
|
1329 |
|
|
1330 |
\end{itemize}
|
|
1331 |
\end{lemma}
|
|
1332 |
\noindent
|
|
1333 |
Here comes the meat of the proof,
|
|
1334 |
which says that once two lists are rewritable to each other,
|
|
1335 |
then they are equivalent under $\rsimp{}$:
|
|
1336 |
\begin{lemma}
|
|
1337 |
If $r_1 \hrewrites r_2$ then $r_1 \sequal r_2$.
|
|
1338 |
\end{lemma}
|
|
1339 |
|
|
1340 |
\noindent
|
|
1341 |
And similar to \ref{Bitcoded2} one can preserve rewritability after taking derivative
|
|
1342 |
of two regular expressions on both sides:
|
|
1343 |
\begin{lemma}\label{interleave}
|
|
1344 |
If $r \hrewrites r' $ then $\rder{c}{r} \hrewrites \rder{c}{r'}$
|
|
1345 |
\end{lemma}
|
|
1346 |
\noindent
|
|
1347 |
This allows proving more $\mathbf{rsimp}$-equivalent terms, involving $\backslash_r$ now.
|
|
1348 |
\begin{lemma}\label{insideSimpRemoval}
|
|
1349 |
$\rsimp{\rder{c}{\rsimp{r}}} = \rsimp{\rder{c}{r}} $
|
|
1350 |
\end{lemma}
|
|
1351 |
\noindent
|
|
1352 |
\begin{proof}
|
|
1353 |
By \ref{interleave} and \ref{rsimpIdem}.
|
|
1354 |
\end{proof}
|
|
1355 |
\noindent
|
|
1356 |
And this unlocks more equivalent terms:
|
|
1357 |
\begin{lemma}\label{Simpders}
|
|
1358 |
As corollaries of \ref{insideSimpRemoval}, we have
|
|
1359 |
\begin{itemize}
|
|
1360 |
\item
|
|
1361 |
If $s \neq []$ then $\rderssimp{r}{s} = \rsimp{(\rders \; r \; s)}$.
|
|
1362 |
\item
|
|
1363 |
$\rsimpalts \; (\map \; (\_ \backslash_r x) \;
|
593
|
1364 |
(\rdistinct{rs}{\varnothing})) \sequal
|
|
1365 |
\rsimpalts \; (\rDistinct \;
|
|
1366 |
(\map \; (\_ \backslash_r x) rs) \;\varnothing )$
|
|
1367 |
\end{itemize}
|
|
1368 |
\end{lemma}
|
611
|
1369 |
\begin{proof}
|
|
1370 |
Part 1 is by lemma \ref{insideSimpRemoval},
|
613
|
1371 |
part 2 is by lemma \ref{insideSimpRemoval} .%and \ref{distinctDer}.
|
611
|
1372 |
\end{proof}
|
557
|
1373 |
\noindent
|
613
|
1374 |
|
|
1375 |
\subsection{Closed Forms for $\sum rs$, $r_1\cdot r_2$ and $r^*$}
|
|
1376 |
\subsubsection{Closed Form for Alternative Regular Expression}
|
|
1377 |
Lemma \ref{Simpders} leads to the first closed form--
|
557
|
1378 |
\begin{lemma}\label{altsClosedForm}
|
593
|
1379 |
\begin{center}
|
|
1380 |
$\rderssimp{(\sum rs)}{s} \sequal
|
|
1381 |
\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
|
|
1382 |
\end{center}
|
557
|
1383 |
\end{lemma}
|
593
|
1384 |
|
556
|
1385 |
\noindent
|
557
|
1386 |
\begin{proof}
|
|
1387 |
By a reverse induction on the string $s$.
|
|
1388 |
One rewriting step, as we mentioned earlier,
|
|
1389 |
involves
|
|
1390 |
\begin{center}
|
|
1391 |
$\rsimpalts \; (\map \; (\_ \backslash x) \;
|
|
1392 |
(\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \;
|
|
1393 |
(\lambda r. \rderssimp{r}{xs}))))}{\varnothing}))
|
|
1394 |
\sequal
|
|
1395 |
\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \;
|
593
|
1396 |
(\rflts \; (\map \; (\rsimp{} \; \circ \;
|
557
|
1397 |
(\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}) $.
|
|
1398 |
\end{center}
|
|
1399 |
This can be proven by a combination of
|
|
1400 |
\ref{grewritesSimpalts}, \ref{gstarRdistinctGeneral}, \ref{rderRsimpAltsCommute}, and
|
|
1401 |
\ref{insideSimpRemoval}.
|
|
1402 |
\end{proof}
|
|
1403 |
\noindent
|
|
1404 |
This closed form has a variant which can be more convenient in later proofs:
|
559
|
1405 |
\begin{corollary}{altsClosedForm1}
|
557
|
1406 |
If $s \neq []$ then
|
|
1407 |
$\rderssimp \; (\sum \; rs) \; s =
|
|
1408 |
\rsimp{(\sum \; (\map \; \rderssimp{\_}{s} \; rs))}$.
|
|
1409 |
\end{corollary}
|
|
1410 |
\noindent
|
|
1411 |
The harder closed forms are the sequence and star ones.
|
613
|
1412 |
Before we obtain them, some preliminary definitions
|
557
|
1413 |
are needed to make proof statements concise.
|
556
|
1414 |
|
609
|
1415 |
|
|
1416 |
\subsubsection{Closed Form for Sequence Regular Expressions}
|
558
|
1417 |
First let's look at a series of derivatives steps on a sequence
|
|
1418 |
regular expression, assuming that each time the first
|
|
1419 |
component of the sequence is always nullable):
|
557
|
1420 |
\begin{center}
|
558
|
1421 |
|
593
|
1422 |
$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$\\
|
|
1423 |
$((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
|
|
1424 |
\ldots$
|
558
|
1425 |
|
557
|
1426 |
\end{center}
|
558
|
1427 |
Roughly speaking $r_1 \cdot r_2 \backslash s$ can be expresssed as
|
|
1428 |
a giant alternative taking a list of terms
|
|
1429 |
$[r_1 \backslash_r s \cdot r_2, r_2 \backslash_r s'', r_2 \backslash_r s_1'', \ldots]$,
|
|
1430 |
where the head of the list is always the term
|
|
1431 |
representing a match involving only $r_1$, and the tail of the list consisting of
|
|
1432 |
terms of the shape $r_2 \backslash_r s''$, $s''$ being a suffix of $s$.
|
557
|
1433 |
This intuition is also echoed by IndianPaper, where they gave
|
|
1434 |
a pencil-and-paper derivation of $(r_1 \cdot r_2)\backslash s$:
|
532
|
1435 |
\begin{center}
|
558
|
1436 |
\begin{tabular}{c}
|
593
|
1437 |
$(r_1 \cdot r_2) \backslash_r (c_1 :: c_2 :: \ldots c_n) \myequiv$\\
|
|
1438 |
\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)
|
|
1439 |
\myequiv$\\
|
|
1440 |
\rule{0pt}{3ex} $((r_1 \backslash_r c_1c_2 \cdot r_2 + (\delta \; (\rnullable \; r_1) \; r_2 \backslash_r c_1c_2))
|
|
1441 |
+ (\delta \ (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)) \backslash_r (c_3 \ldots c_n)
|
|
1442 |
$
|
558
|
1443 |
\end{tabular}
|
557
|
1444 |
\end{center}
|
|
1445 |
\noindent
|
558
|
1446 |
The equality in above should be interpretated
|
|
1447 |
as language equivalence.
|
|
1448 |
The $\delta$ function works similarly to that of
|
|
1449 |
a Kronecker delta function:
|
|
1450 |
\[ \delta \; b\; r\]
|
|
1451 |
will produce $r$
|
|
1452 |
if $b$ evaluates to true,
|
|
1453 |
and $\RZERO$ otherwise.
|
|
1454 |
Note that their formulation
|
|
1455 |
\[
|
|
1456 |
((r_1 \backslash_r \, c_1c_2 \cdot r_2 + (\delta \; (\rnullable) \; r_1, r_2 \backslash_r c_1c_2)
|
|
1457 |
+ (\delta \; (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)
|
|
1458 |
\]
|
|
1459 |
does not faithfully
|
|
1460 |
represent what the intermediate derivatives would actually look like
|
|
1461 |
when one or more intermediate results $r_1 \backslash s' \cdot r_2$ are not
|
|
1462 |
nullable in the head of the sequence.
|
|
1463 |
For example, when $r_1$ and $r_1 \backslash_r c_1$ are not nullable,
|
|
1464 |
the regular expression would not look like
|
|
1465 |
\[
|
|
1466 |
(r_1 \backslash_r c_1c_2 + \RZERO ) + \RZERO,
|
|
1467 |
\]
|
|
1468 |
but actually $r_1 \backslash_r c_1c_2$, the redundant $\RZERO$s will not be created in the
|
|
1469 |
first place.
|
|
1470 |
In a closed-form one would want to take into account this
|
|
1471 |
and generate the list of
|
|
1472 |
regular expressions $r_2 \backslash_r s''$ with
|
|
1473 |
string pairs $(s', s'')$ where $s'@s'' = s$ and
|
|
1474 |
$r_1 \backslash s'$ nullable.
|
|
1475 |
We denote the list consisting of such
|
|
1476 |
strings $s''$ as $\vsuf{s}{r_1}$.
|
|
1477 |
|
|
1478 |
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string:
|
|
1479 |
\begin{center}
|
593
|
1480 |
\begin{tabular}{lcl}
|
|
1481 |
$\vsuf{[]}{\_} $ & $=$ & $[]$\\
|
|
1482 |
$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} (\rnullable{r_1}) \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
|
|
1483 |
&& $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) }) $
|
|
1484 |
\end{tabular}
|
558
|
1485 |
\end{center}
|
|
1486 |
\noindent
|
|
1487 |
The list is sorted in the order $r_2\backslash s''$
|
|
1488 |
appears in $(r_1\cdot r_2)\backslash s$.
|
|
1489 |
In essence, $\vsuf{\_}{\_}$ is doing a
|
|
1490 |
"virtual derivative" of $r_1 \cdot r_2$, but instead of producing
|
|
1491 |
the entire result $(r_1 \cdot r_2) \backslash s$,
|
|
1492 |
it only stores all the strings $s''$ such that $r_2 \backslash s''$
|
|
1493 |
are occurring terms in $(r_1\cdot r_2)\backslash s$.
|
|
1494 |
|
|
1495 |
To make the closed form representation
|
|
1496 |
more straightforward,
|
|
1497 |
the flattetning function $\sflat{\_}$ is used to enable the transformation from
|
557
|
1498 |
a left-associative nested sequence of alternatives into
|
|
1499 |
a flattened list:
|
558
|
1500 |
\[
|
593
|
1501 |
\sum [r_1, r_2, r_3, \ldots] \stackrel{\sflat{\_}}{\rightarrow}
|
|
1502 |
(\ldots ((r_1 + r_2) + r_3) + \ldots)
|
558
|
1503 |
\]
|
|
1504 |
\noindent
|
|
1505 |
The definitions $\sflat{\_}$, $\sflataux{\_}$ are given below.
|
593
|
1506 |
\begin{center}
|
|
1507 |
\begin{tabular}{ccc}
|
|
1508 |
$\sflataux{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
|
|
1509 |
$\sflataux{\AALTS{ }{[]}}$ & $ = $ & $ []$\\
|
|
1510 |
$\sflataux r$ & $=$ & $ [r]$
|
|
1511 |
\end{tabular}
|
532
|
1512 |
\end{center}
|
|
1513 |
|
593
|
1514 |
\begin{center}
|
|
1515 |
\begin{tabular}{ccc}
|
|
1516 |
$\sflat{(\sum r :: rs)}$ & $=$ & $\sum (\sflataux{r} @ rs)$\\
|
|
1517 |
$\sflat{\sum []}$ & $ = $ & $ \sum []$\\
|
|
1518 |
$\sflat r$ & $=$ & $ r$
|
|
1519 |
\end{tabular}
|
557
|
1520 |
\end{center}
|
558
|
1521 |
\noindent
|
576
|
1522 |
$\sflataux{\_}$ breaks up nested alternative regular expressions
|
557
|
1523 |
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
|
558
|
1524 |
into a "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
|
557
|
1525 |
It will return the singleton list $[r]$ otherwise.
|
|
1526 |
$\sflat{\_}$ works the same as $\sflataux{\_}$, except that it keeps
|
|
1527 |
the output type a regular expression, not a list.
|
558
|
1528 |
$\sflataux{\_}$ and $\sflat{\_}$ are only recursive on the
|
|
1529 |
first element of the list.
|
|
1530 |
|
|
1531 |
With $\sflataux{}$ a preliminary to the closed form can be stated,
|
|
1532 |
where the derivative of $r_1 \cdot r_2 \backslash s$ can be
|
|
1533 |
flattened into a list whose head and tail meet the description
|
|
1534 |
we gave earlier.
|
|
1535 |
\begin{lemma}\label{seqSfau0}
|
|
1536 |
$\sflataux{\rders{(r_1 \cdot r_2) \backslash s }} = (r_1 \backslash_r s) \cdot r_2
|
|
1537 |
:: (\map \; (r_2 \backslash_r \_) \; (\textit{Suffix} \; s \; r1))$
|
|
1538 |
\end{lemma}
|
|
1539 |
\begin{proof}
|
|
1540 |
By an induction on the string $s$, where the inductive cases
|
|
1541 |
are split as $[]$ and $xs @ [x]$.
|
|
1542 |
Note the key identify holds:
|
|
1543 |
\[
|
|
1544 |
\map \; (r_2 \backslash_r \_) \; (\vsuf{[x]}{(r_1 \backslash_r xs)}) \;\; @ \;\;
|
|
1545 |
\map \; (\_ \backslash_r x) \; (\map \; (r_2 \backslash \_) \; (\vsuf{xs}{r_1}))
|
|
1546 |
\]
|
593
|
1547 |
=
|
558
|
1548 |
\[
|
|
1549 |
\map \; (r_2 \backslash_r \_) \; (\vsuf{xs @ [x]}{r_1})
|
|
1550 |
\]
|
|
1551 |
This enables the inductive case to go through.
|
|
1552 |
\end{proof}
|
|
1553 |
\noindent
|
|
1554 |
Note that this lemma does $\mathbf{not}$ depend on any
|
|
1555 |
specific definitions we used,
|
|
1556 |
allowing people investigating derivatives to get an alternative
|
|
1557 |
view of what $r_1 \cdot r_2$ is.
|
532
|
1558 |
|
558
|
1559 |
Now we are able to use this for the intuition that
|
|
1560 |
the different ways in which regular expressions are
|
|
1561 |
nested do not matter under $\rsimp{}$:
|
557
|
1562 |
\begin{center}
|
558
|
1563 |
$\rsimp{r} \stackrel{?}{\sequal} \rsimp{r'}$ if $r = \sum [r_1, r_2, r_3, \ldots]$
|
593
|
1564 |
and $r' =(\ldots ((r_1 + r_2) + r_3) + \ldots)$
|
557
|
1565 |
\end{center}
|
558
|
1566 |
Simply wrap with $\sum$ constructor and add
|
|
1567 |
simplifications to both sides of \ref{seqSfau0}
|
|
1568 |
and one gets
|
|
1569 |
\begin{corollary}\label{seqClosedFormGeneral}
|
|
1570 |
$\rsimp{\sflat{(r_1 \cdot r_2) \backslash s} }
|
|
1571 |
=\rsimp{(\sum ( (r_1 \backslash s) \cdot r_2 ::
|
593
|
1572 |
\map\; (r_2 \backslash \_) \; (\vsuf{s}{r_1})))}$
|
558
|
1573 |
\end{corollary}
|
|
1574 |
Together with the idempotency property of $\rsimp{}$ (lemma \ref{rsimpIdem}),
|
|
1575 |
it is possible to convert the above lemma to obtain a "closed form"
|
|
1576 |
for derivatives nested with simplification:
|
|
1577 |
\begin{lemma}\label{seqClosedForm}
|
|
1578 |
$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{(\sum ((r_1 \backslash s) \cdot r_2 )
|
|
1579 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1})))}$
|
|
1580 |
\end{lemma}
|
|
1581 |
\begin{proof}
|
|
1582 |
By a case analysis of string $s$.
|
|
1583 |
When $s$ is empty list, the rewrite is straightforward.
|
|
1584 |
When $s$ is a list, one could use the corollary \ref{seqSfau0},
|
|
1585 |
and lemma \ref{Simpders} to rewrite the left-hand-side.
|
|
1586 |
\end{proof}
|
|
1587 |
As a corollary for this closed form, one can estimate the size
|
|
1588 |
of the sequence derivative $r_1 \cdot r_2 \backslash_r s$ using
|
|
1589 |
an easier-to-handle expression:
|
|
1590 |
\begin{corollary}\label{seqEstimate1}
|
|
1591 |
\begin{center}
|
557
|
1592 |
|
593
|
1593 |
$\llbracket \rderssimp{(r_1 \cdot r_2)}{s} \rrbracket_r = \llbracket \rsimp{(\sum ((r_1 \backslash s) \cdot r_2 )
|
|
1594 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1})))} \rrbracket_r$
|
|
1595 |
|
558
|
1596 |
\end{center}
|
|
1597 |
\end{corollary}
|
|
1598 |
\noindent
|
609
|
1599 |
\subsubsection{Closed Forms for Star Regular Expressions}
|
564
|
1600 |
We have shown how to control the size of the sequence regular expression $r_1\cdot r_2$ using
|
|
1601 |
the "closed form" of $(r_1 \cdot r_2) \backslash s$ and then
|
|
1602 |
the property of the $\distinct$ function.
|
|
1603 |
Now we try to get a bound on $r^* \backslash s$ as well.
|
|
1604 |
Again, we first look at how a star's derivatives evolve, if they grow maximally:
|
|
1605 |
\begin{center}
|
|
1606 |
|
593
|
1607 |
$r^* \quad \longrightarrow_{\backslash c} \quad (r\backslash c) \cdot r^* \quad \longrightarrow_{\backslash c'} \quad
|
|
1608 |
r \backslash cc' \cdot r^* + r \backslash c' \cdot r^* \quad \longrightarrow_{\backslash c''} \quad
|
|
1609 |
(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'''}
|
|
1610 |
\quad \ldots$
|
564
|
1611 |
|
|
1612 |
\end{center}
|
|
1613 |
When we have a string $s = c :: c' :: c'' \ldots$ such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$,
|
|
1614 |
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
|
|
1615 |
the number of terms in $r^* \backslash s$ will grow exponentially, causing the size
|
|
1616 |
of the derivatives $r^* \backslash s$ to grow exponentially, even if we do not
|
|
1617 |
count the possible size explosions of $r \backslash c$ themselves.
|
|
1618 |
|
576
|
1619 |
Thanks to $\rflts$ and $\rDistinct$, we are able to open up regular expressions like
|
564
|
1620 |
$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') +
|
|
1621 |
(r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $
|
|
1622 |
into $\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'',
|
|
1623 |
r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
|
|
1624 |
and then de-duplicate terms of the form $r\backslash s' \cdot r^*$ ($s'$ being a substring of $s$).
|
|
1625 |
This allows us to use a similar technique as $r_1 \cdot r_2$ case,
|
|
1626 |
where the crux is to get an equivalent form of
|
|
1627 |
$\rderssimp{r^*}{s}$ with shape $\rsimp{\sum rs}$.
|
|
1628 |
This requires generating
|
558
|
1629 |
all possible sub-strings $s'$ of $s$
|
|
1630 |
such that $r\backslash s' \cdot r^*$ will appear
|
|
1631 |
as a term in $(r^*) \backslash s$.
|
|
1632 |
The first function we define is a single-step
|
|
1633 |
updating function $\starupdate$, which takes three arguments as input:
|
|
1634 |
the new character $c$ to take derivative with,
|
|
1635 |
the regular expression
|
|
1636 |
$r$ directly under the star $r^*$, and the
|
|
1637 |
list of strings $sSet$ for the derivative $r^* \backslash s$
|
|
1638 |
up til this point
|
|
1639 |
such that $(r^*) \backslash s = \sum_{s' \in sSet} (r\backslash s') \cdot r^*$
|
|
1640 |
(the equality is not exact, more on this later).
|
|
1641 |
\begin{center}
|
|
1642 |
\begin{tabular}{lcl}
|
|
1643 |
$\starupdate \; c \; r \; [] $ & $\dn$ & $[]$\\
|
|
1644 |
$\starupdate \; c \; r \; (s :: Ss)$ & $\dn$ & \\
|
|
1645 |
& & $\textit{if} \;
|
|
1646 |
(\rnullable \; (\rders \; r \; s))$ \\
|
|
1647 |
& & $\textit{then} \;\; (s @ [c]) :: [c] :: (
|
|
1648 |
\starupdate \; c \; r \; Ss)$ \\
|
|
1649 |
& & $\textit{else} \;\; (s @ [c]) :: (
|
|
1650 |
\starupdate \; c \; r \; Ss)$
|
|
1651 |
\end{tabular}
|
|
1652 |
\end{center}
|
|
1653 |
\noindent
|
|
1654 |
As a generalisation from characters to strings,
|
|
1655 |
$\starupdates$ takes a string instead of a character
|
|
1656 |
as the first input argument, and is otherwise the same
|
|
1657 |
as $\starupdate$.
|
|
1658 |
\begin{center}
|
|
1659 |
\begin{tabular}{lcl}
|
|
1660 |
$\starupdates \; [] \; r \; Ss$ & $=$ & $Ss$\\
|
|
1661 |
$\starupdates \; (c :: cs) \; r \; Ss$ & $=$ & $\starupdates \; cs \; r \; (
|
|
1662 |
\starupdate \; c \; r \; Ss)$
|
|
1663 |
\end{tabular}
|
|
1664 |
\end{center}
|
|
1665 |
\noindent
|
|
1666 |
For the star regular expression,
|
|
1667 |
its derivatives can be seen as a nested gigantic
|
|
1668 |
alternative similar to that of sequence regular expression's derivatives,
|
|
1669 |
and therefore need
|
|
1670 |
to be ``straightened out" as well.
|
|
1671 |
The function for this would be $\hflat{}$ and $\hflataux{}$.
|
|
1672 |
\begin{center}
|
|
1673 |
\begin{tabular}{lcl}
|
|
1674 |
$\hflataux{r_1 + r_2}$ & $\dn$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
|
|
1675 |
$\hflataux{r}$ & $\dn$ & $[r]$
|
|
1676 |
\end{tabular}
|
|
1677 |
\end{center}
|
557
|
1678 |
|
|
1679 |
\begin{center}
|
558
|
1680 |
\begin{tabular}{lcl}
|
|
1681 |
$\hflat{r_1 + r_2}$ & $\dn$ & $\sum (\hflataux {r_1} @ \hflataux {r_2}) $\\
|
|
1682 |
$\hflat{r}$ & $\dn$ & $r$
|
|
1683 |
\end{tabular}
|
|
1684 |
\end{center}
|
|
1685 |
\noindent
|
|
1686 |
%MAYBE TODO: introduce createdByStar
|
564
|
1687 |
Again these definitions are tailor-made for dealing with alternatives that have
|
|
1688 |
originated from a star's derivatives, so we do not attempt to open up all possible
|
576
|
1689 |
regular expressions of the form $\RALTS{rs}$, where $\textit{rs}$ might not contain precisely 2
|
564
|
1690 |
elements.
|
|
1691 |
We give a predicate for such "star-created" regular expressions:
|
|
1692 |
\begin{center}
|
593
|
1693 |
\begin{tabular}{lcr}
|
|
1694 |
& & $\createdByStar{(\RSEQ{ra}{\RSTAR{rb}}) }$\\
|
|
1695 |
$\createdByStar{r_1} \land \createdByStar{r_2} $ & $ \Longrightarrow$ & $\createdByStar{(r_1 + r_2)}$
|
|
1696 |
\end{tabular}
|
|
1697 |
\end{center}
|
|
1698 |
|
|
1699 |
These definitions allows us the flexibility to talk about
|
|
1700 |
regular expressions in their most convenient format,
|
|
1701 |
for example, flattened out $\RALTS{[r_1, r_2, \ldots, r_n]} $
|
|
1702 |
instead of binary-nested: $((r_1 + r_2) + (r_3 + r_4)) + \ldots$.
|
|
1703 |
These definitions help express that certain classes of syntatically
|
|
1704 |
distinct regular expressions are actually the same under simplification.
|
|
1705 |
This is not entirely true for annotated regular expressions:
|
|
1706 |
%TODO: bsimp bders \neq bderssimp
|
|
1707 |
\begin{center}
|
|
1708 |
$(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
|
|
1709 |
\end{center}
|
|
1710 |
For bit-codes, the order in which simplification is applied
|
|
1711 |
might cause a difference in the location they are placed.
|
|
1712 |
If we want something like
|
|
1713 |
\begin{center}
|
|
1714 |
$\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
|
|
1715 |
\end{center}
|
|
1716 |
Some "canonicalization" procedure is required,
|
|
1717 |
which either pushes all the common bitcodes to nodes
|
|
1718 |
as senior as possible:
|
|
1719 |
\begin{center}
|
|
1720 |
$_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
|
|
1721 |
\end{center}
|
|
1722 |
or does the reverse. However bitcodes are not of interest if we are talking about
|
|
1723 |
the $\llbracket r \rrbracket$ size of a regex.
|
|
1724 |
Therefore for the ease and simplicity of producing a
|
|
1725 |
proof for a size bound, we are happy to restrict ourselves to
|
|
1726 |
unannotated regular expressions, and obtain such equalities as
|
|
1727 |
\begin{lemma}
|
|
1728 |
$\rsimp{r_1 + r_2} = \rsimp{\RALTS{\hflataux{r_1} @ \hflataux{r_2}}}$
|
|
1729 |
\end{lemma}
|
|
1730 |
|
|
1731 |
\begin{proof}
|
|
1732 |
By using the rewriting relation $\rightsquigarrow$
|
|
1733 |
\end{proof}
|
|
1734 |
%TODO: rsimp sflat
|
564
|
1735 |
And from this we obtain a proof that a star's derivative will be the same
|
|
1736 |
as if it had all its nested alternatives created during deriving being flattened out:
|
593
|
1737 |
For example,
|
|
1738 |
\begin{lemma}
|
|
1739 |
$\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
|
|
1740 |
\end{lemma}
|
|
1741 |
\begin{proof}
|
|
1742 |
By structural induction on $r$, where the induction rules are these of $\createdByStar{_}$.
|
|
1743 |
\end{proof}
|
564
|
1744 |
% The simplification of a flattened out regular expression, provided it comes
|
|
1745 |
%from the derivative of a star, is the same as the one nested.
|
593
|
1746 |
|
564
|
1747 |
|
|
1748 |
|
558
|
1749 |
We first introduce an inductive property
|
|
1750 |
for $\starupdate$ and $\hflataux{\_}$,
|
|
1751 |
it says if we do derivatives of $r^*$
|
|
1752 |
with a string that starts with $c$,
|
|
1753 |
then flatten it out,
|
|
1754 |
we obtain a list
|
|
1755 |
of the shape $\sum_{s' \in sSet} (r\backslash_r s') \cdot r^*$,
|
|
1756 |
where $sSet = \starupdates \; s \; r \; [[c]]$.
|
|
1757 |
\begin{lemma}\label{starHfauInduct}
|
|
1758 |
$\hflataux{(\rders{( (\rder{c}{r_0})\cdot(r_0^*))}{s})} =
|
|
1759 |
\map \; (\lambda s_1. (r_0 \backslash_r s_1) \cdot (r_0^*)) \;
|
|
1760 |
(\starupdates \; s \; r_0 \; [[c]])$
|
|
1761 |
\end{lemma}
|
|
1762 |
\begin{proof}
|
|
1763 |
By an induction on $s$, the inductive cases
|
|
1764 |
being $[]$ and $s@[c]$.
|
|
1765 |
\end{proof}
|
|
1766 |
\noindent
|
|
1767 |
Here is a corollary that states the lemma in
|
|
1768 |
a more intuitive way:
|
|
1769 |
\begin{corollary}
|
|
1770 |
$\hflataux{r^* \backslash_r (c::xs)} = \map \; (\lambda s. (r \backslash_r s) \cdot
|
|
1771 |
(r^*))\; (\starupdates \; c\; r\; [[c]])$
|
|
1772 |
\end{corollary}
|
|
1773 |
\noindent
|
|
1774 |
Note that this is also agnostic of the simplification
|
|
1775 |
function we defined, and is therefore of more general interest.
|
|
1776 |
|
|
1777 |
Now adding the $\rsimp{}$ bit for closed forms,
|
|
1778 |
we have
|
|
1779 |
\begin{lemma}
|
|
1780 |
$a :: rs \grewrites \hflataux{a} @ rs$
|
|
1781 |
\end{lemma}
|
|
1782 |
\noindent
|
|
1783 |
giving us
|
|
1784 |
\begin{lemma}\label{cbsHfauRsimpeq1}
|
|
1785 |
$\rsimp{a+b} = \rsimp{(\sum \hflataux{a} @ \hflataux{b})}$.
|
|
1786 |
\end{lemma}
|
|
1787 |
\noindent
|
|
1788 |
This yields
|
|
1789 |
\begin{lemma}\label{hfauRsimpeq2}
|
593
|
1790 |
$\rsimp{r} = \rsimp{(\sum \hflataux{r})}$
|
558
|
1791 |
\end{lemma}
|
|
1792 |
\noindent
|
|
1793 |
Together with the rewriting relation
|
|
1794 |
\begin{lemma}\label{starClosedForm6Hrewrites}
|
|
1795 |
$\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss
|
|
1796 |
\scfrewrites
|
593
|
1797 |
\map \; (\lambda s. (\rsimp{r \backslash_r s}) \cdot (r^*)) \; Ss$
|
558
|
1798 |
\end{lemma}
|
|
1799 |
\noindent
|
|
1800 |
We obtain the closed form for star regular expression:
|
|
1801 |
\begin{lemma}\label{starClosedForm}
|
|
1802 |
$\rderssimp{r^*}{c::s} =
|
|
1803 |
\rsimp{
|
|
1804 |
(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \;
|
593
|
1805 |
(\starupdates \; s\; r \; [[c]])
|
558
|
1806 |
)
|
593
|
1807 |
)
|
|
1808 |
}
|
558
|
1809 |
$
|
|
1810 |
\end{lemma}
|
|
1811 |
\begin{proof}
|
|
1812 |
By an induction on $s$.
|
|
1813 |
The lemmas \ref{rsimpIdem}, \ref{starHfauInduct}, and \ref{hfauRsimpeq2}
|
|
1814 |
are used.
|
|
1815 |
\end{proof}
|
609
|
1816 |
|
|
1817 |
|
|
1818 |
|
|
1819 |
|
|
1820 |
|
|
1821 |
|
613
|
1822 |
%----------------------------------------------------------------------------------------
|
|
1823 |
% SECTION ??
|
|
1824 |
%----------------------------------------------------------------------------------------
|
|
1825 |
|
|
1826 |
%-----------------------------------
|
|
1827 |
% SECTION syntactic equivalence under simp
|
|
1828 |
%-----------------------------------
|
|
1829 |
|
|
1830 |
|
|
1831 |
%----------------------------------------------------------------------------------------
|
|
1832 |
% SECTION ALTS CLOSED FORM
|
|
1833 |
%----------------------------------------------------------------------------------------
|
|
1834 |
%\section{A Closed Form for \textit{ALTS}}
|
|
1835 |
%Now we prove that $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
|
|
1836 |
%
|
|
1837 |
%
|
|
1838 |
%There are a few key steps, one of these steps is
|
|
1839 |
%
|
|
1840 |
%
|
|
1841 |
%
|
|
1842 |
%One might want to prove this by something a simple statement like:
|
|
1843 |
%
|
|
1844 |
%For this to hold we want the $\textit{distinct}$ function to pick up
|
|
1845 |
%the elements before and after derivatives correctly:
|
|
1846 |
%$r \in rset \equiv (rder x r) \in (rder x rset)$.
|
|
1847 |
%which essentially requires that the function $\backslash$ is an injective mapping.
|
|
1848 |
%
|
|
1849 |
%Unfortunately the function $\backslash c$ is not an injective mapping.
|
|
1850 |
%
|
|
1851 |
%\subsection{function $\backslash c$ is not injective (1-to-1)}
|
|
1852 |
%\begin{center}
|
|
1853 |
% The derivative $w.r.t$ character $c$ is not one-to-one.
|
|
1854 |
% Formally,
|
|
1855 |
% $\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
|
|
1856 |
%\end{center}
|
|
1857 |
%This property is trivially true for the
|
|
1858 |
%character regex example:
|
|
1859 |
%\begin{center}
|
|
1860 |
% $r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
|
|
1861 |
%\end{center}
|
|
1862 |
%But apart from the cases where the derivative
|
|
1863 |
%output is $\ZERO$, are there non-trivial results
|
|
1864 |
%of derivatives which contain strings?
|
|
1865 |
%The answer is yes.
|
|
1866 |
%For example,
|
|
1867 |
%\begin{center}
|
|
1868 |
% Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
|
|
1869 |
% where $a$ is not nullable.\\
|
|
1870 |
% $r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
|
|
1871 |
% $r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
|
|
1872 |
%\end{center}
|
|
1873 |
%We start with two syntactically different regular expressions,
|
|
1874 |
%and end up with the same derivative result.
|
|
1875 |
%This is not surprising as we have such
|
|
1876 |
%equality as below in the style of Arden's lemma:\\
|
|
1877 |
%\begin{center}
|
|
1878 |
% $L(A^*B) = L(A\cdot A^* \cdot B + B)$
|
|
1879 |
%\end{center}
|
|
1880 |
\section{Bounding Closed Forms}
|
|
1881 |
|
|
1882 |
In this section, we introduce how we formalised the bound
|
|
1883 |
on closed forms.
|
|
1884 |
We first prove that functions such as $\rflts$
|
|
1885 |
will not cause the size of r-regular expressions to grow.
|
|
1886 |
Putting this together with a general bound
|
|
1887 |
on the finiteness of distinct regular expressions
|
|
1888 |
smaller than a certain size, we obtain a bound on
|
|
1889 |
the closed forms.
|
|
1890 |
|
|
1891 |
\subsection{$\textit{rsimp}$ Does Not Increment the Size}
|
|
1892 |
Although it seems evident, we need a series
|
|
1893 |
of non-trivial lemmas to establish that functions such as $\rflts$
|
|
1894 |
do not cause the regular expressions to grow.
|
|
1895 |
\begin{lemma}\label{rsimpMonoLemmas}
|
|
1896 |
\mbox{}
|
|
1897 |
\begin{itemize}
|
|
1898 |
\item
|
|
1899 |
\[
|
|
1900 |
\llbracket \rsimpalts \; rs \rrbracket_r \leq
|
|
1901 |
\llbracket \sum \; rs \rrbracket_r
|
|
1902 |
\]
|
|
1903 |
\item
|
|
1904 |
\[
|
|
1905 |
\llbracket \rsimpseq \; r_1 \; r_2 \rrbracket_r \leq
|
|
1906 |
\llbracket r_1 \cdot r_2 \rrbracket_r
|
|
1907 |
\]
|
|
1908 |
\item
|
|
1909 |
\[
|
|
1910 |
\llbracket \rflts \; rs \rrbracket_r \leq
|
|
1911 |
\llbracket rs \rrbracket_r
|
|
1912 |
\]
|
|
1913 |
\item
|
|
1914 |
\[
|
|
1915 |
\llbracket \rDistinct \; rs \; ss \rrbracket_r \leq
|
|
1916 |
\llbracket rs \rrbracket_r
|
|
1917 |
\]
|
|
1918 |
\item
|
|
1919 |
If all elements $a$ in the set $as$ satisfy the property
|
|
1920 |
that $\llbracket \textit{rsimp} \; a \rrbracket_r \leq
|
|
1921 |
\llbracket a \rrbracket_r$, then we have
|
|
1922 |
\[
|
|
1923 |
\llbracket \; \rsimpalts \; (\textit{rdistinct} \;
|
|
1924 |
(\textit{rflts} \; (\textit{map}\;\textit{rsimp} as)) \{\})
|
|
1925 |
\rrbracket \leq
|
|
1926 |
\llbracket \; \sum \; (\rDistinct \; (\rflts \;(\map \;
|
|
1927 |
\textit{rsimp} \; x))\; \{ \} ) \rrbracket_r
|
|
1928 |
\]
|
|
1929 |
\end{itemize}
|
|
1930 |
\end{lemma}
|
|
1931 |
\begin{proof}
|
|
1932 |
Point 1, 3, 4 can be proven by an induction on $rs$.
|
|
1933 |
Point 2 is by case analysis on $r_1$ and $r_2$.
|
|
1934 |
The last part is a corollary of the previous ones.
|
|
1935 |
\end{proof}
|
|
1936 |
\noindent
|
|
1937 |
With the lemmas for each inductive case in place, we are ready to get
|
|
1938 |
the non-increasing property as a corollary:
|
|
1939 |
\begin{corollary}\label{rsimpMono}
|
|
1940 |
$\llbracket \textit{rsimp} \; r \rrbracket_r \leq \llbracket r \rrbracket_r$
|
|
1941 |
\end{corollary}
|
|
1942 |
\begin{proof}
|
|
1943 |
By \ref{rsimpMonoLemmas}.
|
|
1944 |
\end{proof}
|
|
1945 |
|
609
|
1946 |
\subsection{Estimating the Closed Forms' sizes}
|
558
|
1947 |
We now summarize the closed forms below:
|
|
1948 |
\begin{itemize}
|
|
1949 |
\item
|
593
|
1950 |
$\rderssimp{(\sum rs)}{s} \sequal
|
|
1951 |
\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
|
558
|
1952 |
\item
|
593
|
1953 |
$\rderssimp{(r_1 \cdot r_2)}{s} \sequal \sum ((r_1 \backslash s) \cdot r_2 )
|
|
1954 |
:: (\map \; (r_2 \backslash \_) (\vsuf{s}{r_1}))$
|
558
|
1955 |
\item
|
|
1956 |
|
593
|
1957 |
$\rderssimp{r^*}{c::s} =
|
|
1958 |
\rsimp{
|
|
1959 |
(\sum (\map \; (\lambda s. (\rderssimp{r}{s})\cdot r^*) \;
|
558
|
1960 |
(\starupdates \; s\; r \; [[c]])
|
593
|
1961 |
)
|
|
1962 |
)
|
|
1963 |
}
|
|
1964 |
$
|
558
|
1965 |
\end{itemize}
|
|
1966 |
\noindent
|
|
1967 |
The closed forms on the left-hand-side
|
|
1968 |
are all of the same shape: $\rsimp{ (\sum rs)} $.
|
|
1969 |
Such regular expression will be bounded by the size of $\sum rs'$,
|
|
1970 |
where every element in $rs'$ is distinct, and each element
|
|
1971 |
can be described by some inductive sub-structures
|
|
1972 |
(for example when $r = r_1 \cdot r_2$ then $rs'$
|
|
1973 |
will be solely comprised of $r_1 \backslash s'$
|
|
1974 |
and $r_2 \backslash s''$, $s'$ and $s''$ being
|
|
1975 |
sub-strings of $s$).
|
|
1976 |
which will each have a size uppder bound
|
|
1977 |
according to inductive hypothesis, which controls $r \backslash s$.
|
557
|
1978 |
|
558
|
1979 |
We elaborate the above reasoning by a series of lemmas
|
|
1980 |
below, where straightforward proofs are omitted.
|
532
|
1981 |
\begin{lemma}
|
558
|
1982 |
If $\forall r \in rs. \rsize{r} $ is less than or equal to $N$,
|
|
1983 |
and $\textit{length} \; rs$ is less than or equal to $l$,
|
|
1984 |
then $\rsize{\sum rs}$ is less than or equal to $l*N + 1$.
|
|
1985 |
\end{lemma}
|
|
1986 |
\noindent
|
|
1987 |
If we define all regular expressions with size no
|
|
1988 |
more than $N$ as $\sizeNregex \; N$:
|
|
1989 |
\[
|
|
1990 |
\sizeNregex \; N \dn \{r \mid \rsize{r} \leq N \}
|
|
1991 |
\]
|
|
1992 |
Then such set is finite:
|
|
1993 |
\begin{lemma}\label{finiteSizeN}
|
|
1994 |
$\textit{isFinite}\; (\sizeNregex \; N)$
|
|
1995 |
\end{lemma}
|
|
1996 |
\begin{proof}
|
|
1997 |
By overestimating the set $\sizeNregex \; N + 1$
|
|
1998 |
using union of sets like
|
|
1999 |
$\{r_1 \cdot r_2 \mid r_1 \in A
|
|
2000 |
\text{and}
|
593
|
2001 |
r_2 \in A\}
|
558
|
2002 |
$ where $A = \sizeNregex \; N$.
|
|
2003 |
\end{proof}
|
|
2004 |
\noindent
|
|
2005 |
From this we get a corollary that
|
|
2006 |
if forall $r \in rs$, $\rsize{r} \leq N$, then the output of
|
|
2007 |
$\rdistinct{rs}{\varnothing}$ is a list of regular
|
|
2008 |
expressions of finite size depending on $N$ only.
|
561
|
2009 |
\begin{corollary}\label{finiteSizeNCorollary}
|
558
|
2010 |
Assumes that for all $r \in rs. \rsize{r} \leq N$,
|
|
2011 |
and the cardinality of $\sizeNregex \; N$ is $c_N$
|
|
2012 |
then$\rsize{\rdistinct{rs}{\varnothing}} \leq c*N$.
|
|
2013 |
\end{corollary}
|
|
2014 |
\noindent
|
|
2015 |
We have proven that the output of $\rdistinct{rs'}{\varnothing}$
|
|
2016 |
is bounded by a constant $c_N$ depending only on $N$,
|
|
2017 |
provided that each of $rs'$'s element
|
|
2018 |
is bounded by $N$.
|
|
2019 |
We want to apply it to our setting $\rsize{\rsimp{\sum rs}}$.
|
|
2020 |
|
609
|
2021 |
We show that $\rdistinct$ and $\rflts$
|
|
2022 |
working together is at least as
|
|
2023 |
good as $\rdistinct{}{}$ alone, which can be written as
|
|
2024 |
\begin{center}
|
|
2025 |
$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r
|
|
2026 |
\leq
|
|
2027 |
\llbracket \rdistinct{rs}{\varnothing} \rrbracket_r $.
|
|
2028 |
\end{center}
|
|
2029 |
We need this so that we know the outcome of our real
|
|
2030 |
simplification is better than or equal to a rough estimate,
|
|
2031 |
and therefore can be bounded by that estimate.
|
|
2032 |
This is a bit harder to establish compared with proving
|
|
2033 |
$\textit{flts}$ does not make a list larger (which can
|
|
2034 |
be proven using routine induction):
|
|
2035 |
\begin{center}
|
|
2036 |
$\llbracket \textit{rflts}\; rs \rrbracket_r \leq
|
|
2037 |
\llbracket \textit{rs} \rrbracket_r$
|
|
2038 |
\end{center}
|
|
2039 |
We cannot simply prove how each helper function
|
|
2040 |
reduces the size and then put them together:
|
|
2041 |
From
|
|
2042 |
\begin{center}
|
|
2043 |
$\llbracket \textit{rflts}\; rs \rrbracket_r \leq
|
|
2044 |
\llbracket \; \textit{rs} \rrbracket_r$
|
|
2045 |
\end{center}
|
|
2046 |
and
|
|
2047 |
\begin{center}
|
|
2048 |
$\llbracket \textit{rdistinct} \; rs \; \varnothing \leq
|
|
2049 |
\llbracket rs \rrbracket_r$
|
|
2050 |
\end{center}
|
|
2051 |
one cannot imply
|
|
2052 |
\begin{center}
|
558
|
2053 |
$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r
|
|
2054 |
\leq
|
|
2055 |
\llbracket \rdistinct{rs}{\varnothing} \rrbracket_r $.
|
609
|
2056 |
\end{center}
|
|
2057 |
What we can imply is that
|
|
2058 |
\begin{center}
|
|
2059 |
$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r
|
|
2060 |
\leq
|
|
2061 |
\llbracket rs \rrbracket_r$
|
|
2062 |
\end{center}
|
|
2063 |
but this estimate is too rough and $\llbracket rs \rrbracket_r$ is unbounded.
|
|
2064 |
The way we
|
|
2065 |
get through this is by first proving a more general lemma
|
|
2066 |
(so that the inductive case goes through):
|
|
2067 |
\begin{lemma}\label{fltsSizeReductionAlts}
|
|
2068 |
If we have three accumulator sets:
|
|
2069 |
$noalts\_set$, $alts\_set$ and $corr\_set$,
|
|
2070 |
satisfying:
|
|
2071 |
\begin{itemize}
|
|
2072 |
\item
|
|
2073 |
$\forall r \in noalts\_set. \; \nexists xs.\; r = \sum xs$
|
|
2074 |
\item
|
|
2075 |
$\forall r \in alts\_set. \; \exists xs. \; r = \sum xs
|
|
2076 |
\; \textit{and} \; set \; xs \subseteq corr\_set$
|
|
2077 |
\end{itemize}
|
|
2078 |
then we have that
|
|
2079 |
\begin{center}
|
|
2080 |
\begin{tabular}{lcl}
|
|
2081 |
$\llbracket (\textit{rdistinct} \; (\textit{rflts} \; as) \;
|
|
2082 |
(noalts\_set \cup corr\_set)) \rrbracket_r$ & $\leq$ &\\
|
|
2083 |
$\llbracket (\textit{rdistinct} \; as \; (noalts\_set \cup alts\_set \cup
|
|
2084 |
\{ \ZERO \} )) \rrbracket_r$ & & \\
|
|
2085 |
\end{tabular}
|
|
2086 |
\end{center}
|
|
2087 |
holds.
|
532
|
2088 |
\end{lemma}
|
558
|
2089 |
\noindent
|
609
|
2090 |
We need to split the accumulator into two parts: the part
|
|
2091 |
which contains alternative regular expressions ($alts\_set$), and
|
|
2092 |
the part without any of them($noalts\_set$).
|
|
2093 |
The set $corr\_set$ is the corresponding set
|
|
2094 |
of $alts\_set$ with all elements under the $\sum$ constructor
|
|
2095 |
spilled out.
|
|
2096 |
\begin{proof}
|
|
2097 |
By induction on the list $as$. We make use of lemma \ref{rdistinctConcat}.
|
|
2098 |
\end{proof}
|
|
2099 |
By setting all three sets to the empty set, one gets the desired size estimate:
|
|
2100 |
\begin{corollary}\label{interactionFltsDB}
|
|
2101 |
$\llbracket \rdistinct{(\rflts \; \textit{rs})}{\varnothing} \rrbracket_r
|
|
2102 |
\leq
|
|
2103 |
\llbracket \rdistinct{rs}{\varnothing} \rrbracket_r $.
|
|
2104 |
\end{corollary}
|
|
2105 |
\begin{proof}
|
|
2106 |
By using the lemma \ref{fltsSizeReductionAlts}.
|
|
2107 |
\end{proof}
|
|
2108 |
\noindent
|
558
|
2109 |
The intuition is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of
|
|
2110 |
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$.
|
|
2111 |
|
|
2112 |
Now this $\rsimp{\sum rs}$ can be estimated using $\rdistinct{rs}{\varnothing}$:
|
|
2113 |
\begin{lemma}\label{altsSimpControl}
|
|
2114 |
$\rsize{\rsimp{\sum rs}} \leq \rsize{\rdistinct{rs}{\varnothing}}+ 1$
|
532
|
2115 |
\end{lemma}
|
558
|
2116 |
\begin{proof}
|
|
2117 |
By using \ref{interactionFltsDB}.
|
|
2118 |
\end{proof}
|
|
2119 |
\noindent
|
609
|
2120 |
This is a key lemma in establishing the bounds on all the
|
|
2121 |
closed forms.
|
|
2122 |
With this we are now ready to control the sizes of
|
|
2123 |
$(r_1 \cdot r_2 )\backslash s$, $r^* \backslash s$.
|
558
|
2124 |
\begin{theorem}
|
593
|
2125 |
For any regex $r$, $\exists N_r. \forall s. \; \rsize{\rderssimp{r}{s}} \leq N_r$
|
558
|
2126 |
\end{theorem}
|
|
2127 |
\noindent
|
|
2128 |
\begin{proof}
|
593
|
2129 |
We prove this by induction on $r$. The base cases for $\RZERO$,
|
|
2130 |
$\RONE $ and $\RCHAR{c}$ are straightforward.
|
|
2131 |
In the sequence $r_1 \cdot r_2$ case,
|
|
2132 |
the inductive hypotheses state
|
|
2133 |
$\exists N_1. \forall s. \; \llbracket \rderssimp{r}{s} \rrbracket \leq N_1$ and
|
|
2134 |
$\exists N_2. \forall s. \; \llbracket \rderssimp{r_2}{s} \rrbracket \leq N_2$.
|
562
|
2135 |
|
593
|
2136 |
When the string $s$ is not empty, we can reason as follows
|
|
2137 |
%
|
|
2138 |
\begin{center}
|
|
2139 |
\begin{tabular}{lcll}
|
558
|
2140 |
& & $ \llbracket \rderssimp{r_1\cdot r_2 }{s} \rrbracket_r $\\
|
561
|
2141 |
& $ = $ & $\llbracket \rsimp{(\sum(r_1 \backslash_{rsimp} s \cdot r_2 \; \; :: \; \;
|
593
|
2142 |
\map \; (r_2\backslash_{rsimp} \_)\; (\vsuf{s}{r})))} \rrbracket_r $ & (1) \\
|
|
2143 |
& $\leq$ & $\llbracket \rdistinct{(r_1 \backslash_{rsimp} s \cdot r_2 \; \; :: \; \;
|
|
2144 |
\map \; (r_2\backslash_{rsimp} \_)\; (\vsuf{s}{r}))}{\varnothing} \rrbracket_r + 1$ & (2) \\
|
|
2145 |
& $\leq$ & $2 + N_1 + \rsize{r_2} + (N_2 * (card\;(\sizeNregex \; N_2)))$ & (3)\\
|
558
|
2146 |
\end{tabular}
|
|
2147 |
\end{center}
|
561
|
2148 |
\noindent
|
|
2149 |
(1) is by the corollary \ref{seqEstimate1}.
|
|
2150 |
(2) is by \ref{altsSimpControl}.
|
|
2151 |
(3) is by \ref{finiteSizeNCorollary}.
|
562
|
2152 |
|
|
2153 |
|
|
2154 |
Combining the cases when $s = []$ and $s \neq []$, we get (4):
|
|
2155 |
\begin{center}
|
|
2156 |
\begin{tabular}{lcll}
|
|
2157 |
$\rsize{(r_1 \cdot r_2) \backslash_r s}$ & $\leq$ &
|
|
2158 |
$max \; (2 + N_1 +
|
|
2159 |
\llbracket r_2 \rrbracket_r +
|
|
2160 |
N_2 * (card\; (\sizeNregex \; N_2))) \; \rsize{r_1\cdot r_2}$ & (4)
|
|
2161 |
\end{tabular}
|
|
2162 |
\end{center}
|
558
|
2163 |
|
562
|
2164 |
We reason similarly for $\STAR$.
|
|
2165 |
The inductive hypothesis is
|
|
2166 |
$\exists N. \forall s. \; \llbracket \rderssimp{r}{s} \rrbracket \leq N$.
|
564
|
2167 |
Let $n_r = \llbracket r^* \rrbracket_r$.
|
562
|
2168 |
When $s = c :: cs$ is not empty,
|
|
2169 |
\begin{center}
|
593
|
2170 |
\begin{tabular}{lcll}
|
562
|
2171 |
& & $ \llbracket \rderssimp{r^* }{c::cs} \rrbracket_r $\\
|
|
2172 |
& $ = $ & $\llbracket \rsimp{(\sum (\map \; (\lambda s. (r \backslash_{rsimp} s) \cdot r^*) \; (\starupdates\;
|
593
|
2173 |
cs \; r \; [[c]] )) )} \rrbracket_r $ & (5) \\
|
|
2174 |
& $\leq$ & $\llbracket
|
|
2175 |
\rdistinct{
|
|
2176 |
(\map \;
|
|
2177 |
(\lambda s. (r \backslash_{rsimp} s) \cdot r^*) \;
|
|
2178 |
(\starupdates\; cs \; r \; [[c]] )
|
|
2179 |
)}
|
562
|
2180 |
{\varnothing} \rrbracket_r + 1$ & (6) \\
|
|
2181 |
& $\leq$ & $1 + (\textit{card} (\sizeNregex \; (N + n_r)))
|
|
2182 |
* (1 + (N + n_r)) $ & (7)\\
|
|
2183 |
\end{tabular}
|
|
2184 |
\end{center}
|
|
2185 |
\noindent
|
|
2186 |
(5) is by the lemma \ref{starClosedForm}.
|
|
2187 |
(6) is by \ref{altsSimpControl}.
|
|
2188 |
(7) is by \ref{finiteSizeNCorollary}.
|
|
2189 |
Combining with the case when $s = []$, one gets
|
|
2190 |
\begin{center}
|
593
|
2191 |
\begin{tabular}{lcll}
|
|
2192 |
$\rsize{r^* \backslash_r s}$ & $\leq$ & $max \; n_r \; 1 + (\textit{card} (\sizeNregex \; (N + n_r)))
|
|
2193 |
* (1 + (N + n_r)) $ & (8)\\
|
|
2194 |
\end{tabular}
|
562
|
2195 |
\end{center}
|
|
2196 |
\noindent
|
|
2197 |
|
|
2198 |
The alternative case is slightly less involved.
|
|
2199 |
The inductive hypothesis
|
|
2200 |
is equivalent to $\exists N. \forall r \in (\map \; (\_ \backslash_r s) \; rs). \rsize{r} \leq N$.
|
|
2201 |
In the case when $s = c::cs$, we have
|
|
2202 |
\begin{center}
|
593
|
2203 |
\begin{tabular}{lcll}
|
562
|
2204 |
& & $ \llbracket \rderssimp{\sum rs }{c::cs} \rrbracket_r $\\
|
|
2205 |
& $ = $ & $\llbracket \rsimp{(\sum (\map \; (\_ \backslash_{rsimp} s) \; rs) )} \rrbracket_r $ & (9) \\
|
|
2206 |
& $\leq$ & $\llbracket (\sum (\map \; (\_ \backslash_{rsimp} s) \; rs) ) \rrbracket_r $ & (10) \\
|
|
2207 |
& $\leq$ & $1 + N * (length \; rs) $ & (11)\\
|
593
|
2208 |
\end{tabular}
|
562
|
2209 |
\end{center}
|
|
2210 |
\noindent
|
|
2211 |
(9) is by \ref{altsClosedForm}, (10) by \ref{rsimpSize} and (11) by inductive hypothesis.
|
|
2212 |
|
|
2213 |
Combining with the case when $s = []$, one gets
|
|
2214 |
\begin{center}
|
593
|
2215 |
\begin{tabular}{lcll}
|
|
2216 |
$\rsize{\sum rs \backslash_r s}$ & $\leq$ & $max \; \rsize{\sum rs} \; 1+N*(length \; rs)$
|
|
2217 |
& (12)\\
|
|
2218 |
\end{tabular}
|
562
|
2219 |
\end{center}
|
|
2220 |
(4), (8), and (12) are all the inductive cases proven.
|
558
|
2221 |
\end{proof}
|
|
2222 |
|
564
|
2223 |
|
|
2224 |
\begin{corollary}
|
593
|
2225 |
For any regex $a$, $\exists N_r. \forall s. \; \rsize{\bderssimp{a}{s}} \leq N_r$
|
564
|
2226 |
\end{corollary}
|
|
2227 |
\begin{proof}
|
|
2228 |
By \ref{sizeRelations}.
|
|
2229 |
\end{proof}
|
558
|
2230 |
\noindent
|
|
2231 |
|
609
|
2232 |
|
|
2233 |
|
|
2234 |
|
|
2235 |
|
558
|
2236 |
%-----------------------------------
|
|
2237 |
% SECTION 2
|
|
2238 |
%-----------------------------------
|
|
2239 |
|
532
|
2240 |
|
557
|
2241 |
%----------------------------------------------------------------------------------------
|
|
2242 |
% SECTION 3
|
|
2243 |
%----------------------------------------------------------------------------------------
|
|
2244 |
|
532
|
2245 |
|
554
|
2246 |
\subsection{A Closed Form for the Sequence Regular Expression}
|
|
2247 |
\noindent
|
|
2248 |
|
|
2249 |
Before we get to the proof that says the intermediate result of our lexer will
|
|
2250 |
remain finitely bounded, which is an important efficiency/liveness guarantee,
|
|
2251 |
we shall first develop a few preparatory properties and definitions to
|
|
2252 |
make the process of proving that a breeze.
|
|
2253 |
|
|
2254 |
We define rewriting relations for $\rrexp$s, which allows us to do the
|
|
2255 |
same trick as we did for the correctness proof,
|
|
2256 |
but this time we will have stronger equalities established.
|
|
2257 |
|
532
|
2258 |
|
|
2259 |
|
|
2260 |
What guarantee does this bound give us?
|
|
2261 |
|
|
2262 |
Whatever the regex is, it will not grow indefinitely.
|
|
2263 |
Take our previous example $(a + aa)^*$ as an example:
|
|
2264 |
\begin{center}
|
593
|
2265 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
2266 |
\begin{tikzpicture}
|
|
2267 |
\begin{axis}[
|
|
2268 |
xlabel={number of $a$'s},
|
|
2269 |
x label style={at={(1.05,-0.05)}},
|
|
2270 |
ylabel={regex size},
|
|
2271 |
enlargelimits=false,
|
|
2272 |
xtick={0,5,...,30},
|
|
2273 |
xmax=33,
|
|
2274 |
ymax= 40,
|
|
2275 |
ytick={0,10,...,40},
|
|
2276 |
scaled ticks=false,
|
|
2277 |
axis lines=left,
|
|
2278 |
width=5cm,
|
|
2279 |
height=4cm,
|
|
2280 |
legend entries={$(a + aa)^*$},
|
|
2281 |
legend pos=north west,
|
|
2282 |
legend cell align=left]
|
|
2283 |
\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
|
|
2284 |
\end{axis}
|
|
2285 |
\end{tikzpicture}
|
|
2286 |
\end{tabular}
|
532
|
2287 |
\end{center}
|
|
2288 |
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
|
593
|
2289 |
with our simplification
|
532
|
2290 |
rules very effectively.
|
|
2291 |
|
|
2292 |
|
|
2293 |
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
|
|
2294 |
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
|
|
2295 |
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
|
|
2296 |
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
|
|
2297 |
$f(x) = x * 2^x$.
|
|
2298 |
This means the bound we have will surge up at least
|
|
2299 |
tower-exponentially with a linear increase of the depth.
|
|
2300 |
For a regex of depth $n$, the bound
|
|
2301 |
would be approximately $4^n$.
|
|
2302 |
|
|
2303 |
Test data in the graphs from randomly generated regular expressions
|
|
2304 |
shows that the giant bounds are far from being hit.
|
|
2305 |
%a few sample regular experessions' derivatives
|
|
2306 |
%size change
|
576
|
2307 |
%TODO: giving regex1_size_change.data showing a few regular expressions' size changes
|
532
|
2308 |
%w;r;t the input characters number, where the size is usually cubic in terms of original size
|
|
2309 |
%a*, aa*, aaa*, .....
|
576
|
2310 |
%randomly generated regular expressions
|
611
|
2311 |
\begin{figure}{H}
|
593
|
2312 |
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
|
|
2313 |
\begin{tikzpicture}
|
|
2314 |
\begin{axis}[
|
611
|
2315 |
xlabel={number of characters},
|
593
|
2316 |
x label style={at={(1.05,-0.05)}},
|
|
2317 |
ylabel={regex size},
|
|
2318 |
enlargelimits=false,
|
|
2319 |
xtick={0,5,...,30},
|
|
2320 |
xmax=33,
|
611
|
2321 |
%ymax=1000,
|
|
2322 |
%ytick={0,100,...,1000},
|
593
|
2323 |
scaled ticks=false,
|
|
2324 |
axis lines=left,
|
|
2325 |
width=5cm,
|
|
2326 |
height=4cm,
|
|
2327 |
legend entries={regex1},
|
|
2328 |
legend pos=north west,
|
|
2329 |
legend cell align=left]
|
|
2330 |
\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
|
|
2331 |
\end{axis}
|
|
2332 |
\end{tikzpicture}
|
532
|
2333 |
&
|
593
|
2334 |
\begin{tikzpicture}
|
|
2335 |
\begin{axis}[
|
|
2336 |
xlabel={$n$},
|
|
2337 |
x label style={at={(1.05,-0.05)}},
|
|
2338 |
%ylabel={time in secs},
|
|
2339 |
enlargelimits=false,
|
|
2340 |
xtick={0,5,...,30},
|
|
2341 |
xmax=33,
|
611
|
2342 |
%ymax=1000,
|
|
2343 |
%ytick={0,100,...,1000},
|
593
|
2344 |
scaled ticks=false,
|
|
2345 |
axis lines=left,
|
|
2346 |
width=5cm,
|
|
2347 |
height=4cm,
|
|
2348 |
legend entries={regex2},
|
|
2349 |
legend pos=north west,
|
|
2350 |
legend cell align=left]
|
|
2351 |
\addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
|
|
2352 |
\end{axis}
|
|
2353 |
\end{tikzpicture}
|
532
|
2354 |
&
|
593
|
2355 |
\begin{tikzpicture}
|
|
2356 |
\begin{axis}[
|
|
2357 |
xlabel={$n$},
|
|
2358 |
x label style={at={(1.05,-0.05)}},
|
|
2359 |
%ylabel={time in secs},
|
|
2360 |
enlargelimits=false,
|
|
2361 |
xtick={0,5,...,30},
|
|
2362 |
xmax=33,
|
611
|
2363 |
%ymax=1000,
|
|
2364 |
%ytick={0,100,...,1000},
|
593
|
2365 |
scaled ticks=false,
|
|
2366 |
axis lines=left,
|
|
2367 |
width=5cm,
|
|
2368 |
height=4cm,
|
|
2369 |
legend entries={regex3},
|
|
2370 |
legend pos=north west,
|
|
2371 |
legend cell align=left]
|
|
2372 |
\addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
|
|
2373 |
\end{axis}
|
|
2374 |
\end{tikzpicture}\\
|
|
2375 |
\multicolumn{3}{c}{Graphs: size change of 3 randomly generated regular expressions $w.r.t.$ input string length.}
|
|
2376 |
\end{tabular}
|
611
|
2377 |
\end{figure}
|
532
|
2378 |
\noindent
|
|
2379 |
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the
|
|
2380 |
original size.
|
591
|
2381 |
We will discuss improvements to this bound in the next chapter.
|
532
|
2382 |
|
|
2383 |
|
|
2384 |
|
613
|
2385 |
\section{Possible Further Improvements}
|
590
|
2386 |
There are two problems with this finiteness result, though.
|
|
2387 |
\begin{itemize}
|
|
2388 |
\item
|
593
|
2389 |
First, It is not yet a direct formalisation of our lexer's complexity,
|
|
2390 |
as a complexity proof would require looking into
|
|
2391 |
the time it takes to execute {\bf all} the operations
|
|
2392 |
involved in the lexer (simp, collect, decode), not just the derivative.
|
|
2393 |
\item
|
|
2394 |
Second, the bound is not yet tight, and we seek to improve $N_a$ so that
|
|
2395 |
it is polynomial on $\llbracket a \rrbracket$.
|
590
|
2396 |
\end{itemize}
|
|
2397 |
Still, we believe this contribution is fruitful,
|
|
2398 |
because
|
|
2399 |
\begin{itemize}
|
|
2400 |
\item
|
|
2401 |
|
|
2402 |
The size proof can serve as a cornerstone for a complexity
|
|
2403 |
formalisation.
|
|
2404 |
Derivatives are the most important phases of our lexer algorithm.
|
|
2405 |
Size properties about derivatives covers the majority of the algorithm
|
|
2406 |
and is therefore a good indication of complexity of the entire program.
|
|
2407 |
\item
|
|
2408 |
The bound is already a strong indication that catastrophic
|
|
2409 |
backtracking is much less likely to occur in our $\blexersimp$
|
|
2410 |
algorithm.
|
|
2411 |
We refine $\blexersimp$ with $\blexerStrong$ in the next chapter
|
|
2412 |
so that the bound becomes polynomial.
|
|
2413 |
\end{itemize}
|
593
|
2414 |
|
532
|
2415 |
%----------------------------------------------------------------------------------------
|
|
2416 |
% SECTION 4
|
|
2417 |
%----------------------------------------------------------------------------------------
|
593
|
2418 |
|
|
2419 |
|
|
2420 |
|
|
2421 |
|
|
2422 |
|
|
2423 |
|
|
2424 |
|
|
2425 |
|
532
|
2426 |
One might wonder the actual bound rather than the loose bound we gave
|
|
2427 |
for the convenience of an easier proof.
|
|
2428 |
How much can the regex $r^* \backslash s$ grow?
|
|
2429 |
As earlier graphs have shown,
|
|
2430 |
%TODO: reference that graph where size grows quickly
|
593
|
2431 |
they can grow at a maximum speed
|
|
2432 |
exponential $w.r.t$ the number of characters,
|
532
|
2433 |
but will eventually level off when the string $s$ is long enough.
|
|
2434 |
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
|
|
2435 |
would still be slow.
|
|
2436 |
And unfortunately, we have concrete examples
|
576
|
2437 |
where such regular expressions grew exponentially large before levelling off:
|
532
|
2438 |
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
|
|
2439 |
(\underbrace{a \ldots a}_{\text{n a's}})^*$ will already have a maximum
|
593
|
2440 |
size that is exponential on the number $n$
|
532
|
2441 |
under our current simplification rules:
|
|
2442 |
%TODO: graph of a regex whose size increases exponentially.
|
|
2443 |
\begin{center}
|
593
|
2444 |
\begin{tikzpicture}
|
|
2445 |
\begin{axis}[
|
|
2446 |
height=0.5\textwidth,
|
|
2447 |
width=\textwidth,
|
|
2448 |
xlabel=number of a's,
|
|
2449 |
xtick={0,...,9},
|
|
2450 |
ylabel=maximum size,
|
|
2451 |
ymode=log,
|
|
2452 |
log basis y={2}
|
|
2453 |
]
|
|
2454 |
\addplot[mark=*,blue] table {re-chengsong.data};
|
|
2455 |
\end{axis}
|
|
2456 |
\end{tikzpicture}
|
532
|
2457 |
\end{center}
|
|
2458 |
|
|
2459 |
For convenience we use $(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
|
|
2460 |
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
|
|
2461 |
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
|
|
2462 |
The exponential size is triggered by that the regex
|
|
2463 |
$\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
|
|
2464 |
inside the $(\ldots) ^*$ having exponentially many
|
|
2465 |
different derivatives, despite those difference being minor.
|
|
2466 |
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
|
|
2467 |
will therefore contain the following terms (after flattening out all nested
|
|
2468 |
alternatives):
|
|
2469 |
\begin{center}
|
593
|
2470 |
$(\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}})^*)$\\
|
|
2471 |
$(1 \leq m' \leq m )$
|
532
|
2472 |
\end{center}
|
|
2473 |
These terms are distinct for $m' \leq L.C.M.(1, \ldots, n)$ (will be explained in appendix).
|
593
|
2474 |
With each new input character taking the derivative against the intermediate result, more and more such distinct
|
|
2475 |
terms will accumulate,
|
532
|
2476 |
until the length reaches $L.C.M.(1, \ldots, n)$.
|
|
2477 |
$\textit{distinctBy}$ will not be able to de-duplicate any two of these terms
|
|
2478 |
$(\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}})^*)^*$\\
|
|
2479 |
|
|
2480 |
$(\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}})^*)^*$\\
|
593
|
2481 |
where $m' \neq m''$ \\
|
|
2482 |
as they are slightly different.
|
|
2483 |
This means that with our current simplification methods,
|
|
2484 |
we will not be able to control the derivative so that
|
|
2485 |
$\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial %\leq O((\llbracket r\rrbacket)^c)$
|
|
2486 |
as there are already exponentially many terms.
|
|
2487 |
These terms are similar in the sense that the head of those terms
|
|
2488 |
are all consisted of sub-terms of the form:
|
|
2489 |
$(\underbrace{a \ldots a}_{\text{j a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* $.
|
|
2490 |
For $\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
|
|
2491 |
$n * (n + 1) / 2$ such terms.
|
|
2492 |
For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
|
|
2493 |
can be described by 6 terms:
|
|
2494 |
$a^*$, $a\cdot (aa)^*$, $ (aa)^*$,
|
|
2495 |
$aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
|
532
|
2496 |
The total number of different "head terms", $n * (n + 1) / 2$,
|
593
|
2497 |
is proportional to the number of characters in the regex
|
532
|
2498 |
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
|
|
2499 |
This suggests a slightly different notion of size, which we call the
|
|
2500 |
alphabetic width:
|
|
2501 |
%TODO:
|
|
2502 |
(TODO: Alphabetic width def.)
|
|
2503 |
|
593
|
2504 |
|
532
|
2505 |
Antimirov\parencite{Antimirov95} has proven that
|
|
2506 |
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$.
|
|
2507 |
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
|
|
2508 |
created by doing derivatives of $r$ against all possible strings.
|
|
2509 |
If we can make sure that at any moment in our lexing algorithm our
|
|
2510 |
intermediate result hold at most one copy of each of the
|
|
2511 |
subterms then we can get the same bound as Antimirov's.
|
|
2512 |
This leads to the algorithm in the next chapter.
|
|
2513 |
|
|
2514 |
|
|
2515 |
|
|
2516 |
|
|
2517 |
|
|
2518 |
%----------------------------------------------------------------------------------------
|
|
2519 |
% SECTION 1
|
|
2520 |
%----------------------------------------------------------------------------------------
|
|
2521 |
|
|
2522 |
|
|
2523 |
%-----------------------------------
|
|
2524 |
% SUBSECTION 1
|
|
2525 |
%-----------------------------------
|
|
2526 |
\subsection{Syntactic Equivalence Under $\simp$}
|
|
2527 |
We prove that minor differences can be annhilated
|
|
2528 |
by $\simp$.
|
|
2529 |
For example,
|
|
2530 |
\begin{center}
|
593
|
2531 |
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
|
|
2532 |
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
|
532
|
2533 |
\end{center}
|
|
2534 |
|