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