532
+ − 1
% Chapter Template
+ − 2
+ − 3
\chapter{Finiteness Bound} % Main chapter title
+ − 4
+ − 5
\label{Finite}
+ − 6
% In Chapter 4 \ref{Chapter4} we give the second guarantee
+ − 7
%of our bitcoded algorithm, that is a finite bound on the size of any
+ − 8
%regex's derivatives.
+ − 9
+ − 10
In this chapter we give a guarantee in terms of time complexity:
+ − 11
given a regular expression $r$, for any string $s$
+ − 12
our algorithm's internal data structure is finitely bounded.
543
+ − 13
Note that it is not immediately obvious that $\llbracket \bderssimp{r}{s} \rrbracket$ (the internal
+ − 14
data structure used in our $\blexer$)
+ − 15
is bounded by a constant $N_r$, where $N$ only depends on the regular expression
+ − 16
$r$, not the string $s$.
+ − 17
When doing a time complexity analysis of any
+ − 18
lexer/parser based on Brzozowski derivatives, one needs to take into account that
+ − 19
not only the "derivative steps".
+ − 20
+ − 21
%TODO: get a grpah for internal data structure growing arbitrary large
+ − 22
+ − 23
532
+ − 24
To obtain such a proof, we need to
+ − 25
\begin{itemize}
+ − 26
\item
+ − 27
Define an new datatype for regular expressions that makes it easy
+ − 28
to reason about the size of an annotated regular expression.
+ − 29
\item
+ − 30
A set of equalities for this new datatype that enables one to
+ − 31
rewrite $\bderssimp{r_1 \cdot r_2}{s}$ and $\bderssimp{r^*}{s}$ etc.
+ − 32
by their children regexes $r_1$, $r_2$, and $r$.
+ − 33
\item
+ − 34
Using those equalities to actually get those rewriting equations, which we call
+ − 35
"closed forms".
+ − 36
\item
+ − 37
Bound the closed forms, thereby bounding the original
+ − 38
$\blexersimp$'s internal data structures.
+ − 39
\end{itemize}
+ − 40
+ − 41
\section{the $\mathbf{r}$-rexp datatype and the size functions}
+ − 42
+ − 43
We have a size function for bitcoded regular expressions, written
+ − 44
$\llbracket r\rrbracket$, which counts the number of nodes if we regard $r$ as a tree
+ − 45
+ − 46
\begin{center}
+ − 47
\begin{tabular}{ccc}
543
+ − 48
$\llbracket _{bs}\ONE \rrbracket$ & $\dn$ & $1$\\
+ − 49
$\llbracket \ZERO \rrbracket$ & $\dn$ & $1$ \\
+ − 50
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket$ & $\dn$ & $\llbracket r_1 \rrbracket + \llbracket r_2 \rrbracket + 1$\\
+ − 51
$\llbracket _{bs}\mathbf{c} \rrbracket $ & $\dn$ & $1$\\
+ − 52
$\llbracket _{bs}\sum as \rrbracket $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket)\; as + 1$\\
+ − 53
$\llbracket _{bs} a^* \rrbracket $ & $\dn$ & $\llbracket a \rrbracket + 1$
+ − 54
\end{tabular}
+ − 55
\end{center}
+ − 56
+ − 57
\noindent
+ − 58
Similarly there is a size function for plain regular expressions:
+ − 59
\begin{center}
+ − 60
\begin{tabular}{ccc}
+ − 61
$\llbracket \ONE \rrbracket_p$ & $\dn$ & $1$\\
+ − 62
$\llbracket \ZERO \rrbracket_p$ & $\dn$ & $1$ \\
+ − 63
$\llbracket r_1 \cdot r_2 \rrbracket_p$ & $\dn$ & $\llbracket r_1 \rrbracket_p + \llbracket r_2 \rrbracket_p + 1$\\
+ − 64
$\llbracket \mathbf{c} \rrbracket_p $ & $\dn$ & $1$\\
+ − 65
$\llbracket r_1 \cdot r_2 \rrbracket_p $ & $\dn$ & $\llbracket r_1 \rrbracket_p \; + \llbracket r_2 \rrbracket_p + 1$\\
+ − 66
$\llbracket a^* \rrbracket_p $ & $\dn$ & $\llbracket a \rrbracket_p + 1$
532
+ − 67
\end{tabular}
+ − 68
\end{center}
+ − 69
543
+ − 70
\noindent
+ − 71
The idea of obatining a bound for $\llbracket \bderssimp{a}{s} \rrbracket$
+ − 72
is to get an equivalent form
+ − 73
of something like $\llbracket \bderssimp{a}{s}\rrbracket = f(a, s)$, where $f(a, s)$
+ − 74
is easier to estimate than $\llbracket \bderssimp{a}{s}\rrbracket$.
+ − 75
We notice that while it is not so clear how to obtain
+ − 76
a metamorphic representation of $\bderssimp{a}{s}$ (as we argued in chapter \ref{Bitcoded2},
+ − 77
not interleaving the application of the functions $\backslash$ and $\bsimp{\_}$
+ − 78
in the order as our lexer will result in the bit-codes dispensed differently),
+ − 79
it is possible to get an slightly different representation of the unlifted versions:
+ − 80
$ (\bderssimp{a}{s})_\downarrow = (\erase \; \bsimp{a \backslash s})_\downarrow$.
+ − 81
This suggest setting the bounding function $f(a, s)$ as
+ − 82
$\llbracket (a \backslash s)_\downarrow \rrbracket_p$, the plain size
+ − 83
of the erased annotated regular expression.
+ − 84
This requires the the regular expression accompanied by bitcodes
+ − 85
to have the same size as its plain counterpart after erasure:
532
+ − 86
\begin{center}
543
+ − 87
$\asize{a} \stackrel{?}{=} \llbracket \erase(a)\rrbracket_p$.
532
+ − 88
\end{center}
543
+ − 89
\noindent
+ − 90
But there is a minor nuisance:
+ − 91
the erase function unavoidbly messes with the structure of the regular expression,
+ − 92
due to the discrepancy between annotated regular expression's $\sum$ constructor
+ − 93
and plain regular expression's $+$ constructor having different arity.
532
+ − 94
\begin{center}
+ − 95
\begin{tabular}{ccc}
543
+ − 96
$\erase \; _{bs}\sum [] $ & $\dn$ & $\ZERO$\\
+ − 97
$\erase \; _{bs}\sum [a]$ & $\dn$ & $a$\\
+ − 98
$\erase \; _{bs}\sum a :: as$ & $\dn$ & $a + (\erase \; _{[]} \sum as)\quad \text{if $as$ length over 1}$
532
+ − 99
\end{tabular}
+ − 100
\end{center}
543
+ − 101
\noindent
532
+ − 102
An alternative regular expression with an empty list of children
543
+ − 103
is turned into a $\ZERO$ during the
532
+ − 104
$\erase$ function, thereby changing the size and structure of the regex.
543
+ − 105
Therefore the equality in question does not hold.
532
+ − 106
These will likely be fixable if we really want to use plain $\rexp$s for dealing
+ − 107
with size, but we choose a more straightforward (or stupid) method by
+ − 108
defining a new datatype that is similar to plain $\rexp$s but can take
+ − 109
non-binary arguments for its alternative constructor,
+ − 110
which we call $\rrexp$ to denote
+ − 111
the difference between it and plain regular expressions.
+ − 112
\[ \rrexp ::= \RZERO \mid \RONE
+ − 113
\mid \RCHAR{c}
+ − 114
\mid \RSEQ{r_1}{r_2}
+ − 115
\mid \RALTS{rs}
+ − 116
\mid \RSTAR{r}
+ − 117
\]
+ − 118
For $\rrexp$ we throw away the bitcodes on the annotated regular expressions,
+ − 119
but keep everything else intact.
+ − 120
It is similar to annotated regular expressions being $\erase$-ed, but with all its structure preserved
+ − 121
We denote the operation of erasing the bits and turning an annotated regular expression
+ − 122
into an $\rrexp{}$ as $\rerase{}$.
+ − 123
\begin{center}
+ − 124
\begin{tabular}{lcl}
543
+ − 125
$\rerase{\ZERO}$ & $\dn$ & $\RZERO$\\
+ − 126
$\rerase{_{bs}\ONE}$ & $\dn$ & $\RONE$\\
+ − 127
$\rerase{_{bs}\mathbf{c}}$ & $\dn$ & $\RCHAR{c}$\\
+ − 128
$\rerase{_{bs}r_1\cdot r_2}$ & $\dn$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
+ − 129
$\rerase{_{bs}\sum as}$ & $\dn$ & $\RALTS{\map \; \rerase{\_} \; as}$\\
+ − 130
$\rerase{_{bs} a ^*}$ & $\dn$ & $\rerase{a}^*$
532
+ − 131
\end{tabular}
+ − 132
\end{center}
543
+ − 133
\noindent
+ − 134
$\rrexp$ give the exact correspondence between an annotated regular expression
+ − 135
and its (r-)erased version:
+ − 136
\begin{lemma}
+ − 137
$\rsize{\rerase a} = \asize a$
+ − 138
\end{lemma}
554
+ − 139
\noindent
543
+ − 140
This does not hold for plain $\rexp$s.
+ − 141
532
+ − 142
Similarly we could define the derivative and simplification on
+ − 143
$\rrexp$, which would be identical to those we defined for plain $\rexp$s in chapter1,
+ − 144
except that now they can operate on alternatives taking multiple arguments.
+ − 145
+ − 146
\begin{center}
+ − 147
\begin{tabular}{lcr}
543
+ − 148
$(\RALTS{rs})\; \backslash c$ & $\dn$ & $\RALTS{\map\; (\_ \backslash c) \;rs}$\\
532
+ − 149
(other clauses omitted)
543
+ − 150
With the new $\rrexp$ datatype in place, one can define its size function,
+ − 151
which precisely mirrors that of the annotated regular expressions:
+ − 152
\end{tabular}
+ − 153
\end{center}
+ − 154
\noindent
+ − 155
\begin{center}
+ − 156
\begin{tabular}{ccc}
+ − 157
$\llbracket _{bs}\ONE \rrbracket_r$ & $\dn$ & $1$\\
+ − 158
$\llbracket \ZERO \rrbracket_r$ & $\dn$ & $1$ \\
+ − 159
$\llbracket _{bs} r_1 \cdot r_2 \rrbracket_r$ & $\dn$ & $\llbracket r_1 \rrbracket_r + \llbracket r_2 \rrbracket_r + 1$\\
+ − 160
$\llbracket _{bs}\mathbf{c} \rrbracket_r $ & $\dn$ & $1$\\
+ − 161
$\llbracket _{bs}\sum as \rrbracket_r $ & $\dn$ & $\map \; (\llbracket \_ \rrbracket_r)\; as + 1$\\
+ − 162
$\llbracket _{bs} a^* \rrbracket_r $ & $\dn$ & $\llbracket a \rrbracket_r + 1$
532
+ − 163
\end{tabular}
+ − 164
\end{center}
543
+ − 165
\noindent
532
+ − 166
543
+ − 167
\subsection{Lexing Related Functions for $\rrexp$}
+ − 168
Everything else for $\rrexp$ will be precisely the same for annotated expressions,
+ − 169
except that they do not involve rectifying and augmenting bit-encoded tokenization information.
+ − 170
As expected, most functions are simpler, such as the derivative:
+ − 171
\begin{center}
+ − 172
\begin{tabular}{@{}lcl@{}}
+ − 173
$(\ZERO)\,\backslash_r c$ & $\dn$ & $\ZERO$\\
+ − 174
$(\ONE)\,\backslash_r c$ & $\dn$ &
+ − 175
$\textit{if}\;c=d\; \;\textit{then}\;
+ − 176
\ONE\;\textit{else}\;\ZERO$\\
+ − 177
$(\sum \;\textit{rs})\,\backslash_r c$ & $\dn$ &
+ − 178
$\sum\;(\textit{map} \; (\_\backslash_r c) \; rs )$\\
+ − 179
$(r_1\cdot r_2)\,\backslash_r c$ & $\dn$ &
+ − 180
$\textit{if}\;\textit{rnullable}\,r_1$\\
+ − 181
& &$\textit{then}\;\sum\,[(r_1\,\backslash_r c)\cdot\,r_2,$\\
+ − 182
& &$\phantom{\textit{then},\;\sum\,}((r_2\,\backslash_r c))]$\\
+ − 183
& &$\textit{else}\;\,(r_1\,\backslash_r c)\cdot r_2$\\
+ − 184
$(r^*)\,\backslash_r c$ & $\dn$ &
+ − 185
$( r\,\backslash_r c)\cdot
+ − 186
(_{[]}r^*))$
+ − 187
\end{tabular}
+ − 188
\end{center}
+ − 189
\noindent
+ − 190
The simplification function is simplified without annotation causing superficial differences.
+ − 191
Duplicate removal without an equivalence relation:
532
+ − 192
\begin{center}
+ − 193
\begin{tabular}{lcl}
543
+ − 194
$\rdistinct{[]}{rset} $ & $\dn$ & $[]$\\
+ − 195
$\rdistinct{r :: rs}{rset}$ & $\dn$ & $\textit{if}(r \in \textit{rset}) \; \textit{then} \; \rdistinct{rs}{rset}$\\
532
+ − 196
& & $\textit{else}\; r::\rdistinct{rs}{(rset \cup \{r\})}$
+ − 197
\end{tabular}
+ − 198
\end{center}
+ − 199
%TODO: definition of rsimp (maybe only the alternative clause)
543
+ − 200
\noindent
554
+ − 201
The prefix $r$ in front of $\rdistinct{}{}$ is used mainly to
+ − 202
differentiate with $\textit{distinct}$, which is a built-in predicate
+ − 203
in Isabelle that says all the elements of a list are unique.
543
+ − 204
With $\textit{rdistinct}$ one can chain together all the other modules
+ − 205
of $\bsimp{\_}$ (removing the functionalities related to bit-sequences)
+ − 206
and get $\textit{rsimp}$ and $\rderssimp{\_}{\_}$.
+ − 207
We omit these functions, as they are routine. Please refer to the formalisation
+ − 208
(in file BasicIdentities.thy) for the exact definition.
+ − 209
With $\rrexp$ the size caclulation of annotated regular expressions'
+ − 210
simplification and derivatives can be done by the size of their unlifted
+ − 211
counterpart with the unlifted version of simplification and derivatives applied.
532
+ − 212
\begin{lemma}
553
+ − 213
The following equalities hold:
543
+ − 214
\begin{itemize}
+ − 215
\item
554
+ − 216
$\asize{\bsimp{a}} = \rsize{\rsimp{\rerase{a}}}$
+ − 217
\item
+ − 218
$\asize{\bderssimp{r}{s}} = \rsize{\rderssimp{\rerase{r}}{s}}$
+ − 219
\end{itemize}
532
+ − 220
\end{lemma}
543
+ − 221
\noindent
+ − 222
In the following content, we will focus on $\rrexp$'s size bound.
+ − 223
We will piece together this bound and show the same bound for annotated regular
+ − 224
expressions in the end.
532
+ − 225
Unless stated otherwise in this chapter all $\textit{rexp}$s without
+ − 226
bitcodes are seen as $\rrexp$s.
+ − 227
We also use $r_1 + r_2$ and $\RALTS{[r_1, r_2]}$ interchageably
+ − 228
as the former suits people's intuitive way of stating a binary alternative
+ − 229
regular expression.
+ − 230
+ − 231
+ − 232
+ − 233
%-----------------------------------
+ − 234
% SECTION ?
+ − 235
%-----------------------------------
553
+ − 236
\subsection{Finiteness Proof Using $\rrexp$s}
543
+ − 237
Now that we have defined the $\rrexp$ datatype, and proven that its size changes
+ − 238
w.r.t derivatives and simplifications mirrors precisely those of annotated regular expressions,
+ − 239
we aim to bound the size of $r \backslash s$ for any $\rrexp$ $r$.
553
+ − 240
Once we have a bound like:
+ − 241
\[
+ − 242
\llbracket r \backslash_{rsimp} s \rrbracket_r \leq N_r
+ − 243
\]
+ − 244
\noindent
+ − 245
we could easily extend that to
+ − 246
\[
+ − 247
\llbracket a \backslash_{bsimps} s \rrbracket \leq N_r.
+ − 248
\]
+ − 249
+ − 250
\subsection{Roadmap to a Bound for $\textit{Rrexp}$}
+ − 251
+ − 252
The way we obtain the bound for $\rrexp$s is by two steps:
543
+ − 253
\begin{itemize}
+ − 254
\item
+ − 255
First, we rewrite $r\backslash s$ into something else that is easier
+ − 256
to bound. This step is especially important for the inductive case
+ − 257
$r_1 \cdot r_2$ and $r^*$, where the derivative can grow and bloat in a wild way,
+ − 258
but after simplification they will always be equal or smaller to a form consisting of an alternative
+ − 259
list of regular expressions $f \; (g\; (\sum rs))$ with some functions applied to it, where each element will be distinct after the function application.
+ − 260
\item
+ − 261
Then, for such a sum list of regular expressions $f\; (g\; (\sum rs))$, we can control its size
+ − 262
by estimation, since $\distinctBy$ and $\flts$ are well-behaved and working together would only
553
+ − 263
reduce the size of a regular expression, not adding to it.
543
+ − 264
\end{itemize}
+ − 265
553
+ − 266
\section{Step One: Closed Forms}
+ − 267
We transform the function application $\rderssimp{r}{s}$
+ − 268
into an equivalent form $f\; (g \; (\sum rs))$.
+ − 269
The functions $f$ and $g$ can be anything from $\flts$, $\distinctBy$ and other helper functions from $\bsimp{\_}$.
+ − 270
This way we get a different but equivalent way of expressing : $r\backslash s = f \; (g\; (\sum rs))$, we call the
+ − 271
right hand side the "closed form" of $r\backslash s$.
543
+ − 272
\subsection{Basic Properties needed for Closed Forms}
+ − 273
+ − 274
\subsubsection{$\textit{rdistinct}$'s Deduplicates Successfully}
+ − 275
The $\textit{rdistinct}$ function, as its name suggests, will
553
+ − 276
remove duplicates in an \emph{r}$\textit{rexp}$ list,
+ − 277
according to the accumulator
543
+ − 278
and leave only one of each different element in a list:
+ − 279
\begin{lemma}
+ − 280
The function $\textit{rdistinct}$ satisfies the following
+ − 281
properties:
+ − 282
\begin{itemize}
+ − 283
\item
+ − 284
If $a \in acc$ then $a \notin (\rdistinct{rs}{acc})$.
+ − 285
\item
+ − 286
If list $rs'$ is the result of $\rdistinct{rs}{acc}$,
+ − 287
All the elements in $rs'$ are unique.
+ − 288
\end{itemize}
+ − 289
\end{lemma}
+ − 290
\begin{proof}
+ − 291
The first part is by an induction on $rs$.
+ − 292
The second part can be proven by using the
+ − 293
induction rules of $\rdistinct{\_}{\_}$.
+ − 294
+ − 295
\end{proof}
+ − 296
+ − 297
\noindent
+ − 298
$\rdistinct{\_}{\_}$ will cancel out all regular expression terms
+ − 299
that are in the accumulator, therefore prepending a list $rs_a$ with an arbitrary
+ − 300
list $rs$ whose elements are all from the accumulator, and then call $\rdistinct{\_}{\_}$
+ − 301
on the resulting list, the output will be as if we had called $\rdistinct{\_}{\_}$
+ − 302
without the prepending of $rs$:
+ − 303
\begin{lemma}
554
+ − 304
The elements appearing in the accumulator will always be removed.
+ − 305
More precisely,
+ − 306
\begin{itemize}
+ − 307
\item
+ − 308
If $rs \subseteq rset$, then
+ − 309
$\rdistinct{rs@rsa }{acc} = \rdistinct{rsa }{acc}$.
+ − 310
\item
+ − 311
Furthermore, if $a \in rset$ and $\rdistinct{rs}{\{a\}} = []$,
+ − 312
then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{rs'}{rset}$
+ − 313
\end{itemize}
543
+ − 314
\end{lemma}
554
+ − 315
543
+ − 316
\begin{proof}
+ − 317
By induction on $rs$.
+ − 318
\end{proof}
+ − 319
\noindent
+ − 320
On the other hand, if an element $r$ does not appear in the input list waiting to be deduplicated,
+ − 321
then expanding the accumulator to include that element will not cause the output list to change:
+ − 322
\begin{lemma}
+ − 323
The accumulator can be augmented to include elements not appearing in the input list,
+ − 324
and the output will not change.
+ − 325
\begin{itemize}
+ − 326
\item
+ − 327
If $r \notin rs$, then $\rdistinct{rs}{acc} = \rdistinct{rs}{\{r\} \cup acc}$.
+ − 328
\item
+ − 329
Particularly, when $acc = \varnothing$ and $rs$ de-duplicated, we have\\
+ − 330
\[ \rdistinct{rs}{\varnothing} = rs \]
+ − 331
\end{itemize}
+ − 332
\end{lemma}
+ − 333
\begin{proof}
+ − 334
The first half is by induction on $rs$. The second half is a corollary of the first.
+ − 335
\end{proof}
+ − 336
\noindent
+ − 337
The next property gives the condition for
+ − 338
when $\rdistinct{\_}{\_}$ becomes an identical mapping
+ − 339
for any prefix of an input list, in other words, when can
+ − 340
we ``push out" the arguments of $\rdistinct{\_}{\_}$:
+ − 341
\begin{lemma}
554
+ − 342
If $\textit{isDistinct} \; rs_1$, and $rs_1 \cap acc = \varnothing$,
543
+ − 343
then it can be taken out and left untouched in the output:
553
+ − 344
\[\textit{rdistinct}\; (rs_1 @ rsa)\;\, acc
+ − 345
= rs_1@(\textit{rdistinct} rsa \; (acc \cup rs_1))\]
543
+ − 346
\end{lemma}
554
+ − 347
\noindent
+ − 348
The predicate $\textit{isDistinct}$ is for testing
+ − 349
whether a list's elements are all unique. It is defined
+ − 350
recursively on the structure of a regular expression,
+ − 351
and we omit the precise definition here.
543
+ − 352
\begin{proof}
+ − 353
By an induction on $rs_1$, where $rsa$ and $acc$ are allowed to be arbitrary.
+ − 354
\end{proof}
554
+ − 355
\noindent
+ − 356
$\rdistinct{}{}$ removes any element in anywhere of a list, if it
+ − 357
had appeared previously:
+ − 358
\begin{lemma}\label{distinctRemovesMiddle}
+ − 359
The two properties hold if $r \in rs$:
+ − 360
\begin{itemize}
+ − 361
\item
+ − 362
$\rdistinct{rs}{rset} = \rdistinct{(rs @ [r])}{rset}$
+ − 363
and
+ − 364
$\rdistinct{(ab :: rs @ [ab])}{rset'} = \rdistinct{(ab :: rs)}{rset'}$
+ − 365
\item
+ − 366
$\rdistinct{ (rs @ rs') }{rset} = \rdistinct{rs @ [r] @ rs'}{rset}$
+ − 367
and
+ − 368
$\rdistinct{(ab :: rs @ [ab] @ rs'')}{rset'} =
+ − 369
\rdistinct{(ab :: rs @ rs'')}{rset'}$
+ − 370
\end{itemize}
+ − 371
\end{lemma}
+ − 372
\noindent
+ − 373
\begin{proof}
+ − 374
By induction on $rs$. All other variables are allowed to be arbitrary.
+ − 375
The second half of the lemma requires the first half.
+ − 376
Note that for each half's two sub-propositions need to be proven concurrently,
+ − 377
so that the induction goes through.
+ − 378
\end{proof}
+ − 379
553
+ − 380
\subsubsection{The Properties of $\backslash_r$, $\backslash_{rsimp}$, $\textit{Rflts}$ and $\textit{Rsimp}_{ALTS}$}
+ − 381
We give in this subsection some properties of how $\backslash_r$, $\backslash_{rsimp}$, $\textit{Rflts}$ and $\textit{Rsimp}_{ALTS} $ interact with each other and with $@$, the concatenation operator.
543
+ − 382
These will be helpful in later closed form proofs, when
+ − 383
we want to transform the ways in which multiple functions involving
+ − 384
those are composed together
+ − 385
in interleaving derivative and simplification steps.
+ − 386
+ − 387
When the function $\textit{Rflts}$
+ − 388
is applied to the concatenation of two lists, the output can be calculated by first applying the
+ − 389
functions on two lists separately, and then concatenating them together.
554
+ − 390
\begin{lemma}\label{rfltsProps}
543
+ − 391
The function $\rflts$ has the below properties:\\
+ − 392
\begin{itemize}
+ − 393
\item
554
+ − 394
$\rflts \; (rs_1 @ rs_2) = \rflts \; rs_1 @ \rflts \; rs_2$
+ − 395
\item
+ − 396
If $r \neq \RZERO$ and $\nexists rs_1. r = \RALTS{rs}_1$, then $\rflts \; (r::rs) = r :: \rflts \; rs$
+ − 397
\item
+ − 398
$\rflts \; (rs @ [\RZERO]) = \rflts \; rs$
+ − 399
\item
+ − 400
$\rflts \; (rs' @ [\RALTS{rs}]) = \rflts \; rs'@rs$
+ − 401
\item
+ − 402
$\rflts \; (rs @ [\RONE]) = \rflts \; rs @ [\RONE]$
+ − 403
\item
+ − 404
If $r \neq \RZERO$ and $\nexists rs'. r = \RALTS{rs'}$ then $\rflts \; (rs @ [r])
+ − 405
= (\rflts \; rs) @ [r]$
543
+ − 406
\end{itemize}
+ − 407
\end{lemma}
+ − 408
\noindent
+ − 409
\begin{proof}
554
+ − 410
By induction on $rs_1$ in the first part, and induction on $r$ in the second part,
+ − 411
and induction on $rs$, $rs'$ in the third and fourth sub-lemma.
543
+ − 412
\end{proof}
554
+ − 413
\subsubsection{The $RL$ Function: Language Interpretation of $\textit{Rrexp}$s}
+ − 414
Much like the definition of $L$ on plain regular expressions, one could also
+ − 415
define the language interpretation of $\rrexp$s.
+ − 416
\begin{center}
+ − 417
\begin{tabular}{lcl}
+ − 418
$RL \; (\ZERO)$ & $\dn$ & $\phi$\\
+ − 419
$RL \; (\ONE)$ & $\dn$ & $\{[]\}$\\
+ − 420
$RL \; (c)$ & $\dn$ & $\{[c]\}$\\
+ − 421
$RL \; \sum rs$ & $\dn$ & $ \bigcup_{r \in rs} (RL \; r)$\\
+ − 422
$RL \; (r_1 \cdot r_2)$ & $\dn$ & $ RL \; (r_1) @ RL \; (r_2)$\\
+ − 423
$RL \; (r^*)$ & $\dn$ & $ (RL(r))^*$
+ − 424
\end{tabular}
+ − 425
\end{center}
+ − 426
\noindent
+ − 427
The main use of $RL$ is to establish some connections between $\rsimp{}$
+ − 428
and $\rnullable{}$:
+ − 429
\begin{lemma}
+ − 430
The following properties hold:
+ − 431
\begin{itemize}
+ − 432
\item
+ − 433
If $\rnullable{r}$, then $\rsimp{r} \neq \RZERO$.
+ − 434
\item
+ − 435
$\rnullable{r \backslash s} \quad $ if and only if $\quad \rnullable{\rderssimp{r}{s}}$.
+ − 436
\end{itemize}
+ − 437
\end{lemma}
+ − 438
\begin{proof}
+ − 439
The first part is by induction on $r$.
+ − 440
The second part is true because property
+ − 441
\[ RL \; r = RL \; (\rsimp{r})\] holds.
+ − 442
\end{proof}
+ − 443
+ − 444
\subsubsection{$\rsimp{}$ is Non-Increasing}
+ − 445
In this subsection, we prove that the function $\rsimp{}$ does not
+ − 446
make the $\llbracket \rrbracket_r$ size increase.
543
+ − 447
+ − 448
554
+ − 449
\begin{lemma}\label{rsimpSize}
+ − 450
$\llbracket \rsimp{r} \rrbracket_r \leq \llbracket r \rrbracket_r$
+ − 451
\end{lemma}
+ − 452
\subsubsection{Simplified $\textit{Rrexp}$s are Good}
+ − 453
We formalise the notion of ``good" regular expressions,
+ − 454
which means regular expressions that
+ − 455
are not fully simplified. For alternative regular expressions that means they
+ − 456
do not contain any nested alternatives like
+ − 457
\[ r_1 + (r_2 + r_3) \], un-removed $\RZERO$s like \[\RZERO + r\]
+ − 458
or duplicate elements in a children regular expression list like \[ \sum [r, r, \ldots]\]:
+ − 459
\begin{center}
+ − 460
\begin{tabular}{@{}lcl@{}}
+ − 461
$\good\; \RZERO$ & $\dn$ & $\textit{false}$\\
+ − 462
$\good\; \RONE$ & $\dn$ & $\textit{true}$\\
+ − 463
$\good\; \RCHAR{c}$ & $\dn$ & $\btrue$\\
+ − 464
$\good\; \RALTS{[]}$ & $\dn$ & $\bfalse$\\
+ − 465
$\good\; \RALTS{[r]}$ & $\dn$ & $\bfalse$\\
+ − 466
$\good\; \RALTS{r_1 :: r_2 :: rs}$ & $\dn$ &
+ − 467
$\textit{isDistinct} \; (r_1 :: r_2 :: rs) \;$\\
+ − 468
& & $\textit{and}\; (\forall r' \in (r_1 :: r_2 :: rs).\; \good \; r'\; \, \textit{and}\; \, \textit{nonAlt}\; r')$\\
+ − 469
$\good \; \RSEQ{\RZERO}{r}$ & $\dn$ & $\bfalse$\\
+ − 470
$\good \; \RSEQ{\RONE}{r}$ & $\dn$ & $\bfalse$\\
+ − 471
$\good \; \RSEQ{r}{\RZERO}$ & $\dn$ & $\bfalse$\\
+ − 472
$\good \; \RSEQ{r_1}{r_2}$ & $\dn$ & $\good \; r_1 \;\, \textit{and} \;\, \good \; r_2$\\
+ − 473
$\good \; \RSTAR{r}$ & $\dn$ & $\btrue$\\
+ − 474
\end{tabular}
+ − 475
\end{center}
+ − 476
\noindent
+ − 477
The predicate $\textit{nonAlt}$ evaluates to true when the regular expression is not an
+ − 478
alternative, and false otherwise.
+ − 479
The $\good$ property is preserved under $\rsimp_{ALTS}$, provided that
+ − 480
its non-empty argument list of expressions are all good themsleves, and $\textit{nonAlt}$,
+ − 481
and unique:
+ − 482
\begin{lemma}\label{rsimpaltsGood}
+ − 483
If $rs \neq []$ and forall $r \in rs. \textit{nonAlt} \; r$ and $\textit{isDistinct} \; rs$,
+ − 484
then $\good \; (\rsimpalts \; rs)$ if and only if forall $r \in rs. \; \good \; r$.
+ − 485
\end{lemma}
+ − 486
\noindent
+ − 487
We also note that
+ − 488
if a regular expression $r$ is good, then $\rflts$ on the singleton
+ − 489
list $[r]$ will not break goodness:
+ − 490
\begin{lemma}\label{flts2}
+ − 491
If $\good \; r$, then forall $r' \in \rflts \; [r]. \; \good \; r'$ and $\textit{nonAlt} \; r'$.
+ − 492
\end{lemma}
+ − 493
\begin{proof}
+ − 494
By an induction on $r$.
+ − 495
\end{proof}
543
+ − 496
\noindent
554
+ − 497
The other observation we make about $\rsimp{r}$ is that it never
+ − 498
comes with nested alternatives, which we describe as the $\nonnested$
+ − 499
property:
+ − 500
\begin{center}
+ − 501
\begin{tabular}{lcl}
+ − 502
$\nonnested \; \, \sum []$ & $\dn$ & $\btrue$\\
+ − 503
$\nonnested \; \, \sum ((\sum rs_1) :: rs_2)$ & $\dn$ & $\bfalse$\\
+ − 504
$\nonnested \; \, \sum (r :: rs)$ & $\dn$ & $\nonnested (\sum rs)$\\
+ − 505
$\nonnested \; \, r $ & $\dn$ & $\btrue$
+ − 506
\end{tabular}
+ − 507
\end{center}
+ − 508
\noindent
+ − 509
The $\rflts$ function
+ − 510
always opens up nested alternatives,
+ − 511
which enables $\rsimp$ to be non-nested:
+ − 512
+ − 513
\begin{lemma}\label{nonnestedRsimp}
+ − 514
$\nonnested \; (\rsimp{r})$
+ − 515
\end{lemma}
+ − 516
\begin{proof}
+ − 517
By an induction on $r$.
+ − 518
\end{proof}
+ − 519
\noindent
+ − 520
With this we could prove that a regular expressions
+ − 521
after simplification and flattening and de-duplication,
+ − 522
will not contain any alternative regular expression directly:
+ − 523
\begin{lemma}\label{nonaltFltsRd}
+ − 524
If $x \in \rdistinct{\rflts\; (\map \; \rsimp{} \; rs)}{\varnothing}$
+ − 525
then $\textit{nonAlt} \; x$.
+ − 526
\end{lemma}
+ − 527
\begin{proof}
+ − 528
By \ref{nonnestedRsimp}.
+ − 529
\end{proof}
+ − 530
\noindent
+ − 531
The other thing we know is that once $\rsimp{}$ had finished
+ − 532
processing an alternative regular expression, it will not
+ − 533
contain any $\RZERO$s, this is because all the recursive
+ − 534
calls to the simplification on the children regular expressions
+ − 535
make the children good, and $\rflts$ will not take out
+ − 536
any $\RZERO$s out of a good regular expression list,
+ − 537
and $\rdistinct{}$ will not mess with the result.
+ − 538
\begin{lemma}\label{flts3Obv}
+ − 539
The following are true:
+ − 540
\begin{itemize}
+ − 541
\item
+ − 542
If for all $r \in rs. \, \good \; r $ or $r = \RZERO$,
+ − 543
then for all $r \in \rflts\; rs. \, \good \; r$.
+ − 544
\item
+ − 545
If $x \in \rdistinct{\rflts\; (\map \; rsimp{}\; rs)}{\varnothing}$
+ − 546
and for all $y$ such that $\llbracket y \rrbracket_r$ less than
+ − 547
$\llbracket rs \rrbracket_r + 1$, either
+ − 548
$\good \; (\rsimp{y})$ or $\rsimp{y} = \RZERO$,
+ − 549
then $\good \; x$.
+ − 550
\end{itemize}
+ − 551
\end{lemma}
+ − 552
\begin{proof}
+ − 553
The first part is by induction on $rs$, where the induction
+ − 554
rule is the inductive cases for $\rflts$.
+ − 555
The second part is a corollary from the first part.
+ − 556
\end{proof}
543
+ − 557
554
+ − 558
And this leads to good structural property of $\rsimp{}$,
+ − 559
that after simplification, a regular expression is
+ − 560
either good or $\RZERO$:
+ − 561
\begin{lemma}\label{good1}
+ − 562
For any r-regular expression $r$, $\good \; \rsimp{r}$ or $\rsimp{r} = \RZERO$.
+ − 563
\end{lemma}
+ − 564
\begin{proof}
+ − 565
By an induction on $r$. The inductive measure is the size $\llbracket \rrbracket_r$.
+ − 566
Lemma \ref{rsimpSize} says that
+ − 567
$\llbracket \rsimp{r}\rrbracket_r$ is smaller than or equal to
+ − 568
$\llbracket r \rrbracket_r$.
+ − 569
Therefore, in the $r_1 \cdot r_2$ and $\sum rs$ case,
+ − 570
Inductive hypothesis applies to the children regular expressions
+ − 571
$r_1$, $r_2$, etc. The lemma \ref{flts3Obv}'s precondition is satisfied
+ − 572
by that as well.
+ − 573
The lemmas \ref{nonnestedRsimp} and \ref{nonaltFltsRd} are used
+ − 574
to ensure that goodness is preserved at the topmost level.
+ − 575
\end{proof}
+ − 576
We shall prove that any good regular expression is
+ − 577
a fixed-point for $\rsimp{}$.
+ − 578
First we prove an auxiliary lemma:
+ − 579
\begin{lemma}\label{goodaltsNonalt}
+ − 580
If $\good \; \sum rs$, then $\rflts\; rs = rs$.
+ − 581
\end{lemma}
+ − 582
\begin{proof}
+ − 583
By an induction on $\sum rs$. The inductive rules are the cases
+ − 584
for $\good$.
+ − 585
\end{proof}
+ − 586
\noindent
+ − 587
Now we are ready to prove that good regular expressions are invariant
+ − 588
of $\rsimp{}$ application:
+ − 589
\begin{lemma}\label{test}
+ − 590
If $\good \;r$ then $\rsimp{r} = r$.
+ − 591
\end{lemma}
+ − 592
\begin{proof}
+ − 593
By an induction on the inductive cases of $\good$.
+ − 594
The lemma \ref{goodaltsNonalt} is used in the alternative
+ − 595
case where 2 or more elements are present in the list.
+ − 596
\end{proof}
+ − 597
+ − 598
\subsubsection{$\rsimp$ is Idempotent}
+ − 599
The idempotency of $\rsimp$ is very useful in
+ − 600
manipulating regular expression terms into desired
+ − 601
forms so that key steps allowing further rewriting to closed forms
+ − 602
are possible.
+ − 603
\begin{lemma}\label{rsimpIdem}
+ − 604
$\rsimp{r} = \rsimp{\rsimp{r}}$
+ − 605
\end{lemma}
+ − 606
+ − 607
\begin{proof}
+ − 608
By \ref{test} and \ref{good1}.
+ − 609
\end{proof}
+ − 610
\noindent
+ − 611
This property means we do not have to repeatedly
+ − 612
apply simplification in each step, which justifies
+ − 613
our definition of $\blexersimp$.
+ − 614
532
+ − 615
554
+ − 616
On the other hand, we could repeat the same $\rsimp{}$ applications
+ − 617
on regular expressions as many times as we want, if we have at least
+ − 618
one simplification applied to it, and apply it wherever we would like to:
+ − 619
\begin{corollary}\label{headOneMoreSimp}
+ − 620
$\map \; \rsimp{(r :: rs)} = \map \; \rsimp{} \; (\rsimp{r} :: rs)$
+ − 621
\end{corollary}
+ − 622
\noindent
+ − 623
This will be useful in later closed form proof's rewriting steps.
+ − 624
Similarly, we point out the following useful facts below:
+ − 625
\begin{lemma}
+ − 626
The following equalities hold if $r = \rsimp{r'}$ for some $r'$:
+ − 627
\begin{itemize}
+ − 628
\item
+ − 629
If $r = \sum rs$ then $\rsimpalts \; rs = \sum rs$.
+ − 630
\item
+ − 631
If $r = \sum rs$ then $\rdistinct{rs}{\varnothing} = rs$.
+ − 632
\item
+ − 633
$\rsimpalts \; (\rdistinct{\rflts \; [r]}{\varnothing}) = r$.
+ − 634
\end{itemize}
+ − 635
\end{lemma}
+ − 636
\begin{proof}
+ − 637
By application of \ref{rsimpIdem} and \ref{good1}.
+ − 638
\end{proof}
+ − 639
+ − 640
\noindent
+ − 641
With the idempotency of $\rsimp{}$ and its corollaries,
+ − 642
we can start proving some key equalities leading to the
+ − 643
closed forms.
+ − 644
Now presented are a few equivalent terms under $\rsimp{}$.
+ − 645
We use $r_1 \sequal r_2 $ here to denote $\rsimp{r_1} = \rsimp{r_2}$.
+ − 646
\begin{lemma}
+ − 647
\begin{itemize}
+ − 648
\item
+ − 649
$\rsimpalts \; (\RZERO :: rs) \sequal \rsimpalts\; rs$
+ − 650
\item
+ − 651
$\rsimpalts \; rs \sequal \rsimpalts (\map \; \rsimp{} \; rs)$
+ − 652
\item
+ − 653
$\RALTS{\RALTS{rs}} \sequal \RALTS{rs}$
+ − 654
\end{itemize}
+ − 655
\end{lemma}
+ − 656
\noindent
+ − 657
We need more equalities like the above to enable a closed form,
+ − 658
but to proceed we need to introduce two rewrite relations,
+ − 659
to make things smoother.
+ − 660
\subsubsection{The rewrite relation $\hrewrite$ and $\grewrite$}
+ − 661
Insired by the success we had in the correctness proof
+ − 662
in \ref{Bitcoded2}, where we invented
+ − 663
a term rewriting system to capture the similarity between terms
+ − 664
and prove equivalence, we follow suit here defining simplification
+ − 665
steps as rewriting steps.
+ − 666
The presentation will be more concise than that in \ref{Bitcoded2}.
+ − 667
To differentiate between the rewriting steps for annotated regular expressions
+ − 668
and $\rrexp$s, we add characters $h$ and $g$ below the squig arrow symbol
+ − 669
to mean atomic simplification transitions
+ − 670
of $\rrexp$s and $\rrexp$ lists, respectively.
+ − 671
+ − 672
\begin{center}
+ − 673
532
+ − 674
List of 1-step rewrite rules for regular expressions simplification without bitcodes:
+ − 675
\begin{figure}
+ − 676
\caption{the "h-rewrite" rules}
+ − 677
\[
+ − 678
r_1 \cdot \ZERO \rightarrow_h \ZERO \]
+ − 679
+ − 680
\[
+ − 681
\ZERO \cdot r_2 \rightarrow \ZERO
+ − 682
\]
+ − 683
\end{figure}
+ − 684
And we define an "grewrite" relation that works on lists:
+ − 685
\begin{center}
+ − 686
\begin{tabular}{lcl}
+ − 687
$ \ZERO :: rs$ & $\rightarrow_g$ & $rs$
+ − 688
\end{tabular}
+ − 689
\end{center}
+ − 690
+ − 691
+ − 692
+ − 693
With these relations we prove
+ − 694
\begin{lemma}
+ − 695
$rs \rightarrow rs' \implies \RALTS{rs} \rightarrow \RALTS{rs'}$
+ − 696
\end{lemma}
+ − 697
which enables us to prove "closed forms" equalities such as
+ − 698
\begin{lemma}
+ − 699
$\sflat{(r_1 \cdot r_2) \backslash s} = \RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\suffix \; s \; r_1 ))}$
+ − 700
\end{lemma}
+ − 701
+ − 702
But the most involved part of the above lemma is proving the following:
+ − 703
\begin{lemma}
+ − 704
$\bsimp{\RALTS{rs @ \RALTS{rs_1} @ rs'}} = \bsimp{\RALTS{rs @rs_1 @ rs'}} $
+ − 705
\end{lemma}
+ − 706
which is needed in proving things like
+ − 707
\begin{lemma}
+ − 708
$r \rightarrow_f r' \implies \rsimp{r} \rightarrow \rsimp{r'}$
+ − 709
\end{lemma}
+ − 710
+ − 711
Fortunately this is provable by induction on the list $rs$
+ − 712
554
+ − 713
\subsection{A Closed Form for the Sequence Regular Expression}
+ − 714
\begin{quote}\it
+ − 715
Claim: For regular expressions $r_1 \cdot r_2$, we claim that
+ − 716
\begin{center}
+ − 717
$ \rderssimp{r_1 \cdot r_2}{s} =
+ − 718
\rsimp{(\sum (r_1 \backslash s \cdot r_2 ) \; :: \;(\map \; \rderssimp{r2}{\_} \;(\vsuf{s}{r_1})))}$
+ − 719
\end{center}
+ − 720
\end{quote}
+ − 721
\noindent
+ − 722
+ − 723
Before we get to the proof that says the intermediate result of our lexer will
+ − 724
remain finitely bounded, which is an important efficiency/liveness guarantee,
+ − 725
we shall first develop a few preparatory properties and definitions to
+ − 726
make the process of proving that a breeze.
+ − 727
+ − 728
We define rewriting relations for $\rrexp$s, which allows us to do the
+ − 729
same trick as we did for the correctness proof,
+ − 730
but this time we will have stronger equalities established.
+ − 731
532
+ − 732
553
+ − 733
\section{Estimating the Closed Forms' sizes}
532
+ − 734
553
+ − 735
The closed form $f\; (g\; (\sum rs)) $ is controlled
+ − 736
by the size of $rs'$, where every element in $rs'$ is distinct, and each element can be described by som e inductive sub-structures (for example when $r = r_1 \cdot r_2$ then $rs'$ will be solely comprised of $r_1 \backslash s'$ and $r_2 \backslash s''$, $s'$ and $s''$ being sub-strings of $s$), which will have a numeric uppder bound according to inductive hypothesis, which controls $r \backslash s$.
532
+ − 737
%-----------------------------------
+ − 738
% SECTION 2
+ − 739
%-----------------------------------
+ − 740
+ − 741
\begin{theorem}
+ − 742
For any regex $r$, $\exists N_r. \forall s. \; \llbracket{\bderssimp{r}{s}}\rrbracket \leq N_r$
+ − 743
\end{theorem}
+ − 744
+ − 745
\noindent
+ − 746
\begin{proof}
+ − 747
We prove this by induction on $r$. The base cases for $\AZERO$,
+ − 748
$\AONE \textit{bs}$ and $\ACHAR \textit{bs} c$ are straightforward. The interesting case is
+ − 749
for sequences of the form $\ASEQ{bs}{r_1}{r_2}$. In this case our induction
+ − 750
hypotheses state $\exists N_1. \forall s. \; \llbracket \bderssimp{r}{s} \rrbracket \leq N_1$ and
+ − 751
$\exists N_2. \forall s. \; \llbracket \bderssimp{r_2}{s} \rrbracket \leq N_2$. We can reason as follows
+ − 752
%
+ − 753
\begin{center}
+ − 754
\begin{tabular}{lcll}
+ − 755
& & $ \llbracket \bderssimp{\ASEQ{bs}{r_1}{r_2} }{s} \rrbracket$\\
+ − 756
& $ = $ & $\llbracket bsimp\,(\textit{ALTs}\;bs\;(\ASEQ{nil}{\bderssimp{ r_1}{s}}{ r_2} ::
+ − 757
[\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]))\rrbracket $ & (1) \\
+ − 758
& $\leq$ &
+ − 759
$\llbracket\textit{\distinctWith}\,((\ASEQ{nil}{\bderssimp{r_1}{s}}{r_2}$) ::
+ − 760
[$\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)])\,\approx\;{}\rrbracket + 1 $ & (2) \\
+ − 761
& $\leq$ & $\llbracket\ASEQ{bs}{\bderssimp{ r_1}{s}}{r_2}\rrbracket +
+ − 762
\llbracket\textit{distinctWith}\,[\bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]\,\approx\;{}\rrbracket + 1 $ & (3) \\
+ − 763
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 +
+ − 764
\llbracket \distinctWith\,[ \bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)] \,\approx\;\rrbracket$ & (4)\\
+ − 765
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 + l_{N_{2}} * N_{2}$ & (5)
+ − 766
\end{tabular}
+ − 767
\end{center}
+ − 768
+ − 769
+ − 770
\noindent
+ − 771
where in (1) the $\textit{Suffix}( r_1, s)$ are the all the suffixes of $s$ where $\bderssimp{ r_1}{s'}$ is nullable ($s'$ being a suffix of $s$).
+ − 772
The reason why we could write the derivatives of a sequence this way is described in section 2.
+ − 773
The term (2) is used to control (1).
+ − 774
That is because one can obtain an overall
+ − 775
smaller regex list
+ − 776
by flattening it and removing $\ZERO$s first before applying $\distinctWith$ on it.
+ − 777
Section 3 is dedicated to its proof.
+ − 778
In (3) we know that $\llbracket \ASEQ{bs}{(\bderssimp{ r_1}{s}}{r_2}\rrbracket$ is
+ − 779
bounded by $N_1 + \llbracket{}r_2\rrbracket + 1$. In (5) we know the list comprehension contains only regular expressions of size smaller
+ − 780
than $N_2$. The list length after $\distinctWith$ is bounded by a number, which we call $l_{N_2}$. It stands
+ − 781
for the number of distinct regular expressions smaller than $N_2$ (there can only be finitely many of them).
+ − 782
We reason similarly for $\STAR$.\medskip
+ − 783
\end{proof}
+ − 784
+ − 785
What guarantee does this bound give us?
+ − 786
+ − 787
Whatever the regex is, it will not grow indefinitely.
+ − 788
Take our previous example $(a + aa)^*$ as an example:
+ − 789
\begin{center}
+ − 790
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
+ − 791
\begin{tikzpicture}
+ − 792
\begin{axis}[
+ − 793
xlabel={number of $a$'s},
+ − 794
x label style={at={(1.05,-0.05)}},
+ − 795
ylabel={regex size},
+ − 796
enlargelimits=false,
+ − 797
xtick={0,5,...,30},
+ − 798
xmax=33,
+ − 799
ymax= 40,
+ − 800
ytick={0,10,...,40},
+ − 801
scaled ticks=false,
+ − 802
axis lines=left,
+ − 803
width=5cm,
+ − 804
height=4cm,
+ − 805
legend entries={$(a + aa)^*$},
+ − 806
legend pos=north west,
+ − 807
legend cell align=left]
+ − 808
\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
+ − 809
\end{axis}
+ − 810
\end{tikzpicture}
+ − 811
\end{tabular}
+ − 812
\end{center}
+ − 813
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
+ − 814
with our simplification
+ − 815
rules very effectively.
+ − 816
+ − 817
+ − 818
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
+ − 819
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
+ − 820
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
+ − 821
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
+ − 822
$f(x) = x * 2^x$.
+ − 823
This means the bound we have will surge up at least
+ − 824
tower-exponentially with a linear increase of the depth.
+ − 825
For a regex of depth $n$, the bound
+ − 826
would be approximately $4^n$.
+ − 827
+ − 828
Test data in the graphs from randomly generated regular expressions
+ − 829
shows that the giant bounds are far from being hit.
+ − 830
%a few sample regular experessions' derivatives
+ − 831
%size change
+ − 832
%TODO: giving regex1_size_change.data showing a few regexes' size changes
+ − 833
%w;r;t the input characters number, where the size is usually cubic in terms of original size
+ − 834
%a*, aa*, aaa*, .....
+ − 835
%randomly generated regexes
+ − 836
\begin{center}
+ − 837
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
+ − 838
\begin{tikzpicture}
+ − 839
\begin{axis}[
+ − 840
xlabel={number of $a$'s},
+ − 841
x label style={at={(1.05,-0.05)}},
+ − 842
ylabel={regex size},
+ − 843
enlargelimits=false,
+ − 844
xtick={0,5,...,30},
+ − 845
xmax=33,
+ − 846
ymax=1000,
+ − 847
ytick={0,100,...,1000},
+ − 848
scaled ticks=false,
+ − 849
axis lines=left,
+ − 850
width=5cm,
+ − 851
height=4cm,
+ − 852
legend entries={regex1},
+ − 853
legend pos=north west,
+ − 854
legend cell align=left]
+ − 855
\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
+ − 856
\end{axis}
+ − 857
\end{tikzpicture}
+ − 858
&
+ − 859
\begin{tikzpicture}
+ − 860
\begin{axis}[
+ − 861
xlabel={$n$},
+ − 862
x label style={at={(1.05,-0.05)}},
+ − 863
%ylabel={time in secs},
+ − 864
enlargelimits=false,
+ − 865
xtick={0,5,...,30},
+ − 866
xmax=33,
+ − 867
ymax=1000,
+ − 868
ytick={0,100,...,1000},
+ − 869
scaled ticks=false,
+ − 870
axis lines=left,
+ − 871
width=5cm,
+ − 872
height=4cm,
+ − 873
legend entries={regex2},
+ − 874
legend pos=north west,
+ − 875
legend cell align=left]
+ − 876
\addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
+ − 877
\end{axis}
+ − 878
\end{tikzpicture}
+ − 879
&
+ − 880
\begin{tikzpicture}
+ − 881
\begin{axis}[
+ − 882
xlabel={$n$},
+ − 883
x label style={at={(1.05,-0.05)}},
+ − 884
%ylabel={time in secs},
+ − 885
enlargelimits=false,
+ − 886
xtick={0,5,...,30},
+ − 887
xmax=33,
+ − 888
ymax=1000,
+ − 889
ytick={0,100,...,1000},
+ − 890
scaled ticks=false,
+ − 891
axis lines=left,
+ − 892
width=5cm,
+ − 893
height=4cm,
+ − 894
legend entries={regex3},
+ − 895
legend pos=north west,
+ − 896
legend cell align=left]
+ − 897
\addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
+ − 898
\end{axis}
+ − 899
\end{tikzpicture}\\
+ − 900
\multicolumn{3}{c}{Graphs: size change of 3 randomly generated regexes $w.r.t.$ input string length.}
+ − 901
\end{tabular}
+ − 902
\end{center}
+ − 903
+ − 904
+ − 905
+ − 906
+ − 907
+ − 908
\noindent
+ − 909
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the
+ − 910
original size.
+ − 911
This suggests a link towrads "partial derivatives"
+ − 912
introduced by Antimirov \cite{Antimirov95}.
+ − 913
+ − 914
\section{Antimirov's partial derivatives}
+ − 915
The idea behind Antimirov's partial derivatives
+ − 916
is to do derivatives in a similar way as suggested by Brzozowski,
+ − 917
but maintain a set of regular expressions instead of a single one:
+ − 918
+ − 919
%TODO: antimirov proposition 3.1, needs completion
+ − 920
\begin{center}
+ − 921
\begin{tabular}{ccc}
+ − 922
$\partial_x(a+b)$ & $=$ & $\partial_x(a) \cup \partial_x(b)$\\
+ − 923
$\partial_x(\ONE)$ & $=$ & $\phi$
+ − 924
\end{tabular}
+ − 925
\end{center}
+ − 926
+ − 927
Rather than joining the calculated derivatives $\partial_x a$ and $\partial_x b$ together
+ − 928
using the alternatives constructor, Antimirov cleverly chose to put them into
+ − 929
a set instead. This breaks the terms in a derivative regular expression up,
+ − 930
allowing us to understand what are the "atomic" components of it.
+ − 931
For example, To compute what regular expression $x^*(xx + y)^*$'s
+ − 932
derivative against $x$ is made of, one can do a partial derivative
+ − 933
of it and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
+ − 934
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
+ − 935
To get all the "atomic" components of a regular expression's possible derivatives,
+ − 936
there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
+ − 937
whatever character is available at the head of the string inside the language of a
+ − 938
regular expression, and gives back the character and the derivative regular expression
+ − 939
as a pair (which he called "monomial"):
+ − 940
\begin{center}
+ − 941
\begin{tabular}{ccc}
+ − 942
$\lf(\ONE)$ & $=$ & $\phi$\\
+ − 943
$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
+ − 944
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
+ − 945
$\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
+ − 946
\end{tabular}
+ − 947
\end{center}
+ − 948
%TODO: completion
+ − 949
+ − 950
There is a slight difference in the last three clauses compared
+ − 951
with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular
+ − 952
expression $r$ with every element inside $\textit{rset}$ to create a set of
+ − 953
sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates
+ − 954
on a set of monomials (which Antimirov called "linear form") and a regular
+ − 955
expression, and returns a linear form:
+ − 956
\begin{center}
+ − 957
\begin{tabular}{ccc}
+ − 958
$l \bigodot (\ZERO)$ & $=$ & $\phi$\\
+ − 959
$l \bigodot (\ONE)$ & $=$ & $l$\\
+ − 960
$\phi \bigodot t$ & $=$ & $\phi$\\
+ − 961
$\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
+ − 962
$\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
+ − 963
$\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
+ − 964
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
+ − 965
$\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
+ − 966
\end{tabular}
+ − 967
\end{center}
+ − 968
%TODO: completion
+ − 969
+ − 970
Some degree of simplification is applied when doing $\bigodot$, for example,
+ − 971
$l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
+ − 972
and $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
+ − 973
$\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
+ − 974
and so on.
+ − 975
+ − 976
With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regex $r$ with
+ − 977
an iterative procedure:
+ − 978
\begin{center}
+ − 979
\begin{tabular}{llll}
+ − 980
$\textit{while}$ & $(\Delta_i \neq \phi)$ & & \\
+ − 981
& $\Delta_{i+1}$ & $ =$ & $\lf(\Delta_i) - \PD_i$ \\
+ − 982
& $\PD_{i+1}$ & $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
+ − 983
$\partial_{UNIV}(r)$ & $=$ & $\PD$ &
+ − 984
\end{tabular}
+ − 985
\end{center}
+ − 986
+ − 987
+ − 988
$(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
+ − 989
+ − 990
+ − 991
However, if we introduce them in our
+ − 992
setting we would lose the POSIX property of our calculated values.
+ − 993
A simple example for this would be the regex $(a + a\cdot b)\cdot(b\cdot c + c)$.
+ − 994
If we split this regex up into $a\cdot(b\cdot c + c) + a\cdot b \cdot (b\cdot c + c)$, the lexer
+ − 995
would give back $\Left(\Seq(\Char(a), \Left(\Char(b \cdot c))))$ instead of
+ − 996
what we want: $\Seq(\Right(ab), \Right(c))$. Unless we can store the structural information
+ − 997
in all the places where a transformation of the form $(r_1 + r_2)\cdot r \rightarrow r_1 \cdot r + r_2 \cdot r$
+ − 998
occurs, and apply them in the right order once we get
+ − 999
a result of the "aggressively simplified" regex, it would be impossible to still get a $\POSIX$ value.
+ − 1000
This is unlike the simplification we had before, where the rewriting rules
+ − 1001
such as $\ONE \cdot r \rightsquigarrow r$, under which our lexer will give the same value.
+ − 1002
We will discuss better
+ − 1003
bounds in the last section of this chapter.\\[-6.5mm]
+ − 1004
+ − 1005
+ − 1006
+ − 1007
+ − 1008
%----------------------------------------------------------------------------------------
+ − 1009
% SECTION ??
+ − 1010
%----------------------------------------------------------------------------------------
+ − 1011
+ − 1012
\section{"Closed Forms" of regular expressions' derivatives w.r.t strings}
+ − 1013
To embark on getting the "closed forms" of regexes, we first
+ − 1014
define a few auxiliary definitions to make expressing them smoothly.
+ − 1015
+ − 1016
\begin{center}
+ − 1017
\begin{tabular}{ccc}
+ − 1018
$\sflataux{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
+ − 1019
$\sflataux{\AALTS{ }{[]}}$ & $ = $ & $ []$\\
+ − 1020
$\sflataux r$ & $=$ & $ [r]$
+ − 1021
\end{tabular}
+ − 1022
\end{center}
+ − 1023
The intuition behind $\sflataux{\_}$ is to break up nested regexes
+ − 1024
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
+ − 1025
into a more "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
+ − 1026
It will return the singleton list $[r]$ otherwise.
+ − 1027
+ − 1028
$\sflat{\_}$ works the same as $\sflataux{\_}$, except that it keeps
+ − 1029
the output type a regular expression, not a list:
+ − 1030
\begin{center}
+ − 1031
\begin{tabular}{ccc}
+ − 1032
$\sflat{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
+ − 1033
$\sflat{\AALTS{ }{[]}}$ & $ = $ & $ \AALTS{ }{[]}$\\
+ − 1034
$\sflat r$ & $=$ & $ [r]$
+ − 1035
\end{tabular}
+ − 1036
\end{center}
+ − 1037
$\sflataux{\_}$ and $\sflat{\_}$ is only recursive in terms of the
+ − 1038
first element of the list of children of
+ − 1039
an alternative regex ($\AALTS{ }{rs}$), and is purposefully built for dealing with the regular expression
+ − 1040
$r_1 \cdot r_2 \backslash s$.
+ − 1041
+ − 1042
With $\sflat{\_}$ and $\sflataux{\_}$ we make
+ − 1043
precise what "closed forms" we have for the sequence derivatives and their simplifications,
+ − 1044
in other words, how can we construct $(r_1 \cdot r_2) \backslash s$
+ − 1045
and $\bderssimp{(r_1\cdot r_2)}{s}$,
+ − 1046
if we are only allowed to use a combination of $r_1 \backslash s'$ ($\bderssimp{r_1}{s'}$)
+ − 1047
and $r_2 \backslash s'$ ($\bderssimp{r_2}{s'}$), where $s'$ ranges over
+ − 1048
the substring of $s$?
+ − 1049
First let's look at a series of derivatives steps on a sequence
+ − 1050
regular expression, (assuming) that each time the first
+ − 1051
component of the sequence is always nullable):
+ − 1052
\begin{center}
+ − 1053
+ − 1054
$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$\\
+ − 1055
$((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
+ − 1056
\ldots$
+ − 1057
+ − 1058
\end{center}
+ − 1059
%TODO: cite indian paper
+ − 1060
Indianpaper have come up with a slightly more formal way of putting the above process:
+ − 1061
\begin{center}
+ − 1062
$r_1 \cdot r_2 \backslash (c_1 :: c_2 ::\ldots c_n) \myequiv r_1 \backslash (c_1 :: c_2:: \ldots c_n) +
+ − 1063
\delta(\nullable(r_1 \backslash (c_1 :: c_2 \ldots c_{n-1}) ), r_2 \backslash (c_n)) + \ldots
+ − 1064
+ \delta (\nullable(r_1), r_2\backslash (c_1 :: c_2 ::\ldots c_n))$
+ − 1065
\end{center}
+ − 1066
where $\delta(b, r)$ will produce $r$
+ − 1067
if $b$ evaluates to true,
+ − 1068
and $\ZERO$ otherwise.
+ − 1069
+ − 1070
But the $\myequiv$ sign in the above equation means language equivalence instead of syntactical
+ − 1071
equivalence. To make this intuition useful
+ − 1072
for a formal proof, we need something
+ − 1073
stronger than language equivalence.
+ − 1074
With the help of $\sflat{\_}$ we can state the equation in Indianpaper
+ − 1075
more rigorously:
+ − 1076
\begin{lemma}
+ − 1077
$\sflat{(r_1 \cdot r_2) \backslash s} = \RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}$
+ − 1078
\end{lemma}
+ − 1079
+ − 1080
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string:
+ − 1081
+ − 1082
\begin{center}
+ − 1083
\begin{tabular}{lcl}
+ − 1084
$\vsuf{[]}{\_} $ & $=$ & $[]$\\
+ − 1085
$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} (\rnullable{r_1}) \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
+ − 1086
&& $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) }) $
+ − 1087
\end{tabular}
+ − 1088
\end{center}
+ − 1089
It takes into account which prefixes $s'$ of $s$ would make $r \backslash s'$ nullable,
+ − 1090
and keep a list of suffixes $s''$ such that $s' @ s'' = s$. The list is sorted in
+ − 1091
the order $r_2\backslash s''$ appears in $(r_1\cdot r_2)\backslash s$.
+ − 1092
In essence, $\vsuf{\_}{\_}$ is doing a "virtual derivative" of $r_1 \cdot r_2$, but instead of producing
+ − 1093
the entire result of $(r_1 \cdot r_2) \backslash s$,
+ − 1094
it only stores all the $s''$ such that $r_2 \backslash s''$ are occurring terms in $(r_1\cdot r_2)\backslash s$.
+ − 1095
+ − 1096
With this we can also add simplifications to both sides and get
+ − 1097
\begin{lemma}
+ − 1098
$\rsimp{\sflat{(r_1 \cdot r_2) \backslash s} }= \rsimp{\AALTS{[[]}{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}}$
+ − 1099
\end{lemma}
554
+ − 1100
Together with the idempotency property of $\rsimp{}$\ref{rsimpIdem},
532
+ − 1101
%TODO: state the idempotency property of rsimp
+ − 1102
it is possible to convert the above lemma to obtain a "closed form"
+ − 1103
for derivatives nested with simplification:
+ − 1104
\begin{lemma}
+ − 1105
$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{\AALTS{[[]}{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}}$
+ − 1106
\end{lemma}
+ − 1107
And now the reason we have (1) in section 1 is clear:
+ − 1108
$\rsize{\rsimp{\RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map \;(r_2 \backslash \_) \; (\vsuf{s}{r_1}))}}}$,
+ − 1109
is equal to $\rsize{\rsimp{\RALTS{ ((r_1 \backslash s) \cdot r_2 :: (\map \; (r_2 \backslash \_) \; (\textit{Suffix}(r1, s))))}}}$
+ − 1110
as $\vsuf{s}{r_1}$ is one way of expressing the list $\textit{Suffix}( r_1, s)$.
+ − 1111
+ − 1112
%----------------------------------------------------------------------------------------
+ − 1113
% SECTION 3
+ − 1114
%----------------------------------------------------------------------------------------
+ − 1115
+ − 1116
\section{interaction between $\distinctWith$ and $\flts$}
+ − 1117
Note that it is not immediately obvious that
+ − 1118
\begin{center}
+ − 1119
$\llbracket \distinctWith (\flts \textit{rs}) = \phi \rrbracket \leq \llbracket \distinctWith \textit{rs} = \phi \rrbracket $.
+ − 1120
\end{center}
+ − 1121
The intuition is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of
+ − 1122
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$.
+ − 1123
+ − 1124
+ − 1125
%-----------------------------------
+ − 1126
% SECTION syntactic equivalence under simp
+ − 1127
%-----------------------------------
+ − 1128
\section{Syntactic Equivalence Under $\simp$}
+ − 1129
We prove that minor differences can be annhilated
+ − 1130
by $\simp$.
+ − 1131
For example,
+ − 1132
\begin{center}
+ − 1133
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
+ − 1134
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
+ − 1135
\end{center}
+ − 1136
+ − 1137
+ − 1138
%----------------------------------------------------------------------------------------
+ − 1139
% SECTION ALTS CLOSED FORM
+ − 1140
%----------------------------------------------------------------------------------------
+ − 1141
\section{A Closed Form for \textit{ALTS}}
+ − 1142
Now we prove that $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
+ − 1143
+ − 1144
+ − 1145
There are a few key steps, one of these steps is
+ − 1146
\[
+ − 1147
rsimp (rsimp\_ALTs (map (rder x) (rdistinct (rflts (map (rsimp \circ (\lambda r. rders\_simp r xs)) rs)) {})))
+ − 1148
= rsimp (rsimp\_ALTs (rdistinct (map (rder x) (rflts (map (rsimp \circ (\lambda r. rders\_simp r xs)) rs))) {}))
+ − 1149
\]
+ − 1150
+ − 1151
+ − 1152
One might want to prove this by something a simple statement like:
+ − 1153
$map (rder x) (rdistinct rs rset) = rdistinct (map (rder x) rs) (rder x) ` rset)$.
+ − 1154
+ − 1155
For this to hold we want the $\textit{distinct}$ function to pick up
+ − 1156
the elements before and after derivatives correctly:
+ − 1157
$r \in rset \equiv (rder x r) \in (rder x rset)$.
+ − 1158
which essentially requires that the function $\backslash$ is an injective mapping.
+ − 1159
+ − 1160
Unfortunately the function $\backslash c$ is not an injective mapping.
+ − 1161
+ − 1162
\subsection{function $\backslash c$ is not injective (1-to-1)}
+ − 1163
\begin{center}
+ − 1164
The derivative $w.r.t$ character $c$ is not one-to-one.
+ − 1165
Formally,
+ − 1166
$\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
+ − 1167
\end{center}
+ − 1168
This property is trivially true for the
+ − 1169
character regex example:
+ − 1170
\begin{center}
+ − 1171
$r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
+ − 1172
\end{center}
+ − 1173
But apart from the cases where the derivative
+ − 1174
output is $\ZERO$, are there non-trivial results
+ − 1175
of derivatives which contain strings?
+ − 1176
The answer is yes.
+ − 1177
For example,
+ − 1178
\begin{center}
+ − 1179
Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
+ − 1180
where $a$ is not nullable.\\
+ − 1181
$r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
+ − 1182
$r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
+ − 1183
\end{center}
+ − 1184
We start with two syntactically different regexes,
+ − 1185
and end up with the same derivative result.
+ − 1186
This is not surprising as we have such
+ − 1187
equality as below in the style of Arden's lemma:\\
+ − 1188
\begin{center}
+ − 1189
$L(A^*B) = L(A\cdot A^* \cdot B + B)$
+ − 1190
\end{center}
+ − 1191
+ − 1192
%----------------------------------------------------------------------------------------
+ − 1193
% SECTION 4
+ − 1194
%----------------------------------------------------------------------------------------
+ − 1195
\section{A Bound for the Star Regular Expression}
+ − 1196
We have shown how to control the size of the sequence regular expression $r_1\cdot r_2$ using
+ − 1197
the "closed form" of $(r_1 \cdot r_2) \backslash s$ and then
+ − 1198
the property of the $\distinct$ function.
+ − 1199
Now we try to get a bound on $r^* \backslash s$ as well.
+ − 1200
Again, we first look at how a star's derivatives evolve, if they grow maximally:
+ − 1201
\begin{center}
+ − 1202
+ − 1203
$r^* \quad \longrightarrow_{\backslash c} \quad (r\backslash c) \cdot r^* \quad \longrightarrow_{\backslash c'} \quad
+ − 1204
r \backslash cc' \cdot r^* + r \backslash c' \cdot r^* \quad \longrightarrow_{\backslash c''} \quad
+ − 1205
(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'''}
+ − 1206
\quad \ldots$
+ − 1207
+ − 1208
\end{center}
+ − 1209
When we have a string $s = c :: c' :: c'' \ldots$ such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$,
+ − 1210
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
+ − 1211
the number of terms in $r^* \backslash s$ will grow exponentially, causing the size
+ − 1212
of the derivatives $r^* \backslash s$ to grow exponentially, even if we do not
+ − 1213
count the possible size explosions of $r \backslash c$ themselves.
+ − 1214
+ − 1215
Thanks to $\flts$ and $\distinctWith$, we are able to open up regexes like
+ − 1216
$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + (r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $
+ − 1217
into $\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'', r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
+ − 1218
and then de-duplicate terms of the form $r\backslash s' \cdot r^*$ ($s'$ being a substring of $s$).
+ − 1219
For this we define $\hflataux{\_}$ and $\hflat{\_}$, similar to $\sflataux{\_}$ and $\sflat{\_}$:
+ − 1220
%TODO: definitions of and \hflataux \hflat
+ − 1221
\begin{center}
+ − 1222
\begin{tabular}{ccc}
+ − 1223
$\hflataux{r_1 + r_2}$ & $=$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
+ − 1224
$\hflataux r$ & $=$ & $ [r]$
+ − 1225
\end{tabular}
+ − 1226
\end{center}
+ − 1227
+ − 1228
\begin{center}
+ − 1229
\begin{tabular}{ccc}
+ − 1230
$\hflat{r_1 + r_2}$ & $=$ & $\RALTS{\hflataux{r_1} @ \hflataux{r_2}}$\\
+ − 1231
$\hflat r$ & $=$ & $ r$
+ − 1232
\end{tabular}
+ − 1233
\end{center}s
+ − 1234
Again these definitions are tailor-made for dealing with alternatives that have
+ − 1235
originated from a star's derivatives, so we don't attempt to open up all possible
+ − 1236
regexes of the form $\RALTS{rs}$, where $\textit{rs}$ might not contain precisely 2
+ − 1237
elements.
+ − 1238
We give a predicate for such "star-created" regular expressions:
+ − 1239
\begin{center}
+ − 1240
\begin{tabular}{lcr}
+ − 1241
& & $\createdByStar{(\RSEQ{ra}{\RSTAR{rb}}) }$\\
+ − 1242
$\createdByStar{r_1} \land \createdByStar{r_2} $ & $ \Longrightarrow$ & $\createdByStar{(r_1 + r_2)}$
+ − 1243
\end{tabular}
+ − 1244
\end{center}
+ − 1245
+ − 1246
These definitions allows us the flexibility to talk about
+ − 1247
regular expressions in their most convenient format,
+ − 1248
for example, flattened out $\RALTS{[r_1, r_2, \ldots, r_n]} $
+ − 1249
instead of binary-nested: $((r_1 + r_2) + (r_3 + r_4)) + \ldots$.
+ − 1250
These definitions help express that certain classes of syntatically
+ − 1251
distinct regular expressions are actually the same under simplification.
+ − 1252
This is not entirely true for annotated regular expressions:
+ − 1253
%TODO: bsimp bders \neq bderssimp
+ − 1254
\begin{center}
+ − 1255
$(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
+ − 1256
\end{center}
+ − 1257
For bit-codes, the order in which simplification is applied
+ − 1258
might cause a difference in the location they are placed.
+ − 1259
If we want something like
+ − 1260
\begin{center}
+ − 1261
$\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
+ − 1262
\end{center}
+ − 1263
Some "canonicalization" procedure is required,
+ − 1264
which either pushes all the common bitcodes to nodes
+ − 1265
as senior as possible:
+ − 1266
\begin{center}
+ − 1267
$_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
+ − 1268
\end{center}
+ − 1269
or does the reverse. However bitcodes are not of interest if we are talking about
+ − 1270
the $\llbracket r \rrbracket$ size of a regex.
+ − 1271
Therefore for the ease and simplicity of producing a
+ − 1272
proof for a size bound, we are happy to restrict ourselves to
+ − 1273
unannotated regular expressions, and obtain such equalities as
+ − 1274
\begin{lemma}
+ − 1275
$\rsimp{r_1 + r_2} = \rsimp{\RALTS{\hflataux{r_1} @ \hflataux{r_2}}}$
+ − 1276
\end{lemma}
+ − 1277
+ − 1278
\begin{proof}
+ − 1279
By using the rewriting relation $\rightsquigarrow$
+ − 1280
\end{proof}
+ − 1281
%TODO: rsimp sflat
+ − 1282
And from this we obtain a proof that a star's derivative will be the same
+ − 1283
as if it had all its nested alternatives created during deriving being flattened out:
+ − 1284
For example,
+ − 1285
\begin{lemma}
+ − 1286
$\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
+ − 1287
\end{lemma}
+ − 1288
\begin{proof}
+ − 1289
By structural induction on $r$, where the induction rules are these of $\createdByStar{_}$.
+ − 1290
\end{proof}
+ − 1291
% The simplification of a flattened out regular expression, provided it comes
+ − 1292
%from the derivative of a star, is the same as the one nested.
+ − 1293
+ − 1294
+ − 1295
+ − 1296
+ − 1297
+ − 1298
+ − 1299
+ − 1300
+ − 1301
+ − 1302
One might wonder the actual bound rather than the loose bound we gave
+ − 1303
for the convenience of an easier proof.
+ − 1304
How much can the regex $r^* \backslash s$ grow?
+ − 1305
As earlier graphs have shown,
+ − 1306
%TODO: reference that graph where size grows quickly
+ − 1307
they can grow at a maximum speed
+ − 1308
exponential $w.r.t$ the number of characters,
+ − 1309
but will eventually level off when the string $s$ is long enough.
+ − 1310
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
+ − 1311
would still be slow.
+ − 1312
And unfortunately, we have concrete examples
+ − 1313
where such regexes grew exponentially large before levelling off:
+ − 1314
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
+ − 1315
(\underbrace{a \ldots a}_{\text{n a's}})^*$ will already have a maximum
+ − 1316
size that is exponential on the number $n$
+ − 1317
under our current simplification rules:
+ − 1318
%TODO: graph of a regex whose size increases exponentially.
+ − 1319
\begin{center}
+ − 1320
\begin{tikzpicture}
+ − 1321
\begin{axis}[
+ − 1322
height=0.5\textwidth,
+ − 1323
width=\textwidth,
+ − 1324
xlabel=number of a's,
+ − 1325
xtick={0,...,9},
+ − 1326
ylabel=maximum size,
+ − 1327
ymode=log,
+ − 1328
log basis y={2}
+ − 1329
]
+ − 1330
\addplot[mark=*,blue] table {re-chengsong.data};
+ − 1331
\end{axis}
+ − 1332
\end{tikzpicture}
+ − 1333
\end{center}
+ − 1334
+ − 1335
For convenience we use $(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
+ − 1336
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
+ − 1337
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
+ − 1338
The exponential size is triggered by that the regex
+ − 1339
$\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
+ − 1340
inside the $(\ldots) ^*$ having exponentially many
+ − 1341
different derivatives, despite those difference being minor.
+ − 1342
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
+ − 1343
will therefore contain the following terms (after flattening out all nested
+ − 1344
alternatives):
+ − 1345
\begin{center}
+ − 1346
$(\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}})^*)$\\
+ − 1347
$(1 \leq m' \leq m )$
+ − 1348
\end{center}
+ − 1349
These terms are distinct for $m' \leq L.C.M.(1, \ldots, n)$ (will be explained in appendix).
+ − 1350
With each new input character taking the derivative against the intermediate result, more and more such distinct
+ − 1351
terms will accumulate,
+ − 1352
until the length reaches $L.C.M.(1, \ldots, n)$.
+ − 1353
$\textit{distinctBy}$ will not be able to de-duplicate any two of these terms
+ − 1354
$(\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}})^*)^*$\\
+ − 1355
+ − 1356
$(\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}})^*)^*$\\
+ − 1357
where $m' \neq m''$ \\
+ − 1358
as they are slightly different.
+ − 1359
This means that with our current simplification methods,
+ − 1360
we will not be able to control the derivative so that
+ − 1361
$\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial %\leq O((\llbracket r\rrbacket)^c)$
+ − 1362
as there are already exponentially many terms.
+ − 1363
These terms are similar in the sense that the head of those terms
+ − 1364
are all consisted of sub-terms of the form:
+ − 1365
$(\underbrace{a \ldots a}_{\text{j a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* $.
+ − 1366
For $\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
+ − 1367
$n * (n + 1) / 2$ such terms.
+ − 1368
For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
+ − 1369
can be described by 6 terms:
+ − 1370
$a^*$, $a\cdot (aa)^*$, $ (aa)^*$,
+ − 1371
$aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
+ − 1372
The total number of different "head terms", $n * (n + 1) / 2$,
+ − 1373
is proportional to the number of characters in the regex
+ − 1374
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
+ − 1375
This suggests a slightly different notion of size, which we call the
+ − 1376
alphabetic width:
+ − 1377
%TODO:
+ − 1378
(TODO: Alphabetic width def.)
+ − 1379
+ − 1380
+ − 1381
Antimirov\parencite{Antimirov95} has proven that
+ − 1382
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$.
+ − 1383
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
+ − 1384
created by doing derivatives of $r$ against all possible strings.
+ − 1385
If we can make sure that at any moment in our lexing algorithm our
+ − 1386
intermediate result hold at most one copy of each of the
+ − 1387
subterms then we can get the same bound as Antimirov's.
+ − 1388
This leads to the algorithm in the next chapter.
+ − 1389
+ − 1390
+ − 1391
+ − 1392
+ − 1393
+ − 1394
%----------------------------------------------------------------------------------------
+ − 1395
% SECTION 1
+ − 1396
%----------------------------------------------------------------------------------------
+ − 1397
+ − 1398
+ − 1399
%-----------------------------------
+ − 1400
% SUBSECTION 1
+ − 1401
%-----------------------------------
+ − 1402
\subsection{Syntactic Equivalence Under $\simp$}
+ − 1403
We prove that minor differences can be annhilated
+ − 1404
by $\simp$.
+ − 1405
For example,
+ − 1406
\begin{center}
+ − 1407
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
+ − 1408
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
+ − 1409
\end{center}
+ − 1410