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:
555
+ − 279
\begin{lemma}\label{rdistinctDoesTheJob}
543
+ − 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}$,
555
+ − 287
then $\textit{isDistinct} \; rs'$.
+ − 288
\item
+ − 289
$\rdistinct{rs}{acc} = rs - acc$
543
+ − 290
\end{itemize}
+ − 291
\end{lemma}
555
+ − 292
\noindent
+ − 293
The predicate $\textit{isDistinct}$ is for testing
+ − 294
whether a list's elements are all unique. It is defined
+ − 295
recursively on the structure of a regular expression,
+ − 296
and we omit the precise definition here.
543
+ − 297
\begin{proof}
+ − 298
The first part is by an induction on $rs$.
555
+ − 299
The second and third part can be proven by using the
543
+ − 300
induction rules of $\rdistinct{\_}{\_}$.
+ − 301
+ − 302
\end{proof}
+ − 303
+ − 304
\noindent
+ − 305
$\rdistinct{\_}{\_}$ will cancel out all regular expression terms
+ − 306
that are in the accumulator, therefore prepending a list $rs_a$ with an arbitrary
+ − 307
list $rs$ whose elements are all from the accumulator, and then call $\rdistinct{\_}{\_}$
+ − 308
on the resulting list, the output will be as if we had called $\rdistinct{\_}{\_}$
+ − 309
without the prepending of $rs$:
+ − 310
\begin{lemma}
554
+ − 311
The elements appearing in the accumulator will always be removed.
+ − 312
More precisely,
+ − 313
\begin{itemize}
+ − 314
\item
+ − 315
If $rs \subseteq rset$, then
+ − 316
$\rdistinct{rs@rsa }{acc} = \rdistinct{rsa }{acc}$.
+ − 317
\item
+ − 318
Furthermore, if $a \in rset$ and $\rdistinct{rs}{\{a\}} = []$,
+ − 319
then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{rs'}{rset}$
+ − 320
\end{itemize}
543
+ − 321
\end{lemma}
554
+ − 322
543
+ − 323
\begin{proof}
+ − 324
By induction on $rs$.
+ − 325
\end{proof}
+ − 326
\noindent
+ − 327
On the other hand, if an element $r$ does not appear in the input list waiting to be deduplicated,
+ − 328
then expanding the accumulator to include that element will not cause the output list to change:
+ − 329
\begin{lemma}
+ − 330
The accumulator can be augmented to include elements not appearing in the input list,
+ − 331
and the output will not change.
+ − 332
\begin{itemize}
+ − 333
\item
+ − 334
If $r \notin rs$, then $\rdistinct{rs}{acc} = \rdistinct{rs}{\{r\} \cup acc}$.
+ − 335
\item
+ − 336
Particularly, when $acc = \varnothing$ and $rs$ de-duplicated, we have\\
+ − 337
\[ \rdistinct{rs}{\varnothing} = rs \]
+ − 338
\end{itemize}
+ − 339
\end{lemma}
+ − 340
\begin{proof}
+ − 341
The first half is by induction on $rs$. The second half is a corollary of the first.
+ − 342
\end{proof}
+ − 343
\noindent
+ − 344
The next property gives the condition for
+ − 345
when $\rdistinct{\_}{\_}$ becomes an identical mapping
+ − 346
for any prefix of an input list, in other words, when can
+ − 347
we ``push out" the arguments of $\rdistinct{\_}{\_}$:
555
+ − 348
\begin{lemma}\label{distinctRdistinctAppend}
554
+ − 349
If $\textit{isDistinct} \; rs_1$, and $rs_1 \cap acc = \varnothing$,
555
+ − 350
then
553
+ − 351
\[\textit{rdistinct}\; (rs_1 @ rsa)\;\, acc
+ − 352
= rs_1@(\textit{rdistinct} rsa \; (acc \cup rs_1))\]
543
+ − 353
\end{lemma}
554
+ − 354
\noindent
555
+ − 355
In other words, it can be taken out and left untouched in the output.
543
+ − 356
\begin{proof}
+ − 357
By an induction on $rs_1$, where $rsa$ and $acc$ are allowed to be arbitrary.
+ − 358
\end{proof}
554
+ − 359
\noindent
+ − 360
$\rdistinct{}{}$ removes any element in anywhere of a list, if it
+ − 361
had appeared previously:
+ − 362
\begin{lemma}\label{distinctRemovesMiddle}
+ − 363
The two properties hold if $r \in rs$:
+ − 364
\begin{itemize}
+ − 365
\item
555
+ − 366
$\rdistinct{rs}{rset} = \rdistinct{(rs @ [r])}{rset}$\\
+ − 367
and\\
554
+ − 368
$\rdistinct{(ab :: rs @ [ab])}{rset'} = \rdistinct{(ab :: rs)}{rset'}$
+ − 369
\item
555
+ − 370
$\rdistinct{ (rs @ rs') }{rset} = \rdistinct{rs @ [r] @ rs'}{rset}$\\
+ − 371
and\\
554
+ − 372
$\rdistinct{(ab :: rs @ [ab] @ rs'')}{rset'} =
+ − 373
\rdistinct{(ab :: rs @ rs'')}{rset'}$
+ − 374
\end{itemize}
+ − 375
\end{lemma}
+ − 376
\noindent
+ − 377
\begin{proof}
+ − 378
By induction on $rs$. All other variables are allowed to be arbitrary.
+ − 379
The second half of the lemma requires the first half.
+ − 380
Note that for each half's two sub-propositions need to be proven concurrently,
+ − 381
so that the induction goes through.
+ − 382
\end{proof}
+ − 383
555
+ − 384
\noindent
+ − 385
This allows us to prove ``Idempotency" of $\rdistinct{}{}$ of some kind:
+ − 386
\begin{lemma}\label{rdistinctConcatGeneral}
+ − 387
The following equalities involving multiple applications of $\rdistinct{}{}$ hold:
+ − 388
\begin{itemize}
+ − 389
\item
+ − 390
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{((\rdistinct{rs}{\varnothing})@ rs')}{\varnothing}$
+ − 391
\item
+ − 392
$\rdistinct{(rs @ rs')}{\varnothing} = \rdistinct{(\rdistinct{rs}{\varnothing} @ rs')}{\varnothing}$
+ − 393
\item
+ − 394
If $rset' \subseteq rset$, then $\rdistinct{rs}{rset} =
+ − 395
\rdistinct{(\rdistinct{rs}{rset'})}{rset}$. As a corollary
+ − 396
of this,
+ − 397
\item
+ − 398
$\rdistinct{(rs @ rs')}{rset} = \rdistinct{
+ − 399
(\rdistinct{rs}{\varnothing}) @ rs')}{rset}$. This
+ − 400
gives another corollary use later:
+ − 401
\item
+ − 402
If $a \in rset$, then $\rdistinct{(rs @ rs')}{rset} = \rdistinct{
+ − 403
(\rdistinct{(a :: rs)}{\varnothing} @ rs')}{rset} $,
+ − 404
+ − 405
\end{itemize}
+ − 406
\end{lemma}
+ − 407
\begin{proof}
+ − 408
By \ref{rdistinctDoesTheJob} and \ref{distinctRemovesMiddle}.
+ − 409
\end{proof}
+ − 410
553
+ − 411
\subsubsection{The Properties of $\backslash_r$, $\backslash_{rsimp}$, $\textit{Rflts}$ and $\textit{Rsimp}_{ALTS}$}
+ − 412
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
+ − 413
These will be helpful in later closed form proofs, when
+ − 414
we want to transform the ways in which multiple functions involving
+ − 415
those are composed together
+ − 416
in interleaving derivative and simplification steps.
+ − 417
+ − 418
When the function $\textit{Rflts}$
+ − 419
is applied to the concatenation of two lists, the output can be calculated by first applying the
+ − 420
functions on two lists separately, and then concatenating them together.
554
+ − 421
\begin{lemma}\label{rfltsProps}
543
+ − 422
The function $\rflts$ has the below properties:\\
+ − 423
\begin{itemize}
+ − 424
\item
554
+ − 425
$\rflts \; (rs_1 @ rs_2) = \rflts \; rs_1 @ \rflts \; rs_2$
+ − 426
\item
+ − 427
If $r \neq \RZERO$ and $\nexists rs_1. r = \RALTS{rs}_1$, then $\rflts \; (r::rs) = r :: \rflts \; rs$
+ − 428
\item
+ − 429
$\rflts \; (rs @ [\RZERO]) = \rflts \; rs$
+ − 430
\item
+ − 431
$\rflts \; (rs' @ [\RALTS{rs}]) = \rflts \; rs'@rs$
+ − 432
\item
+ − 433
$\rflts \; (rs @ [\RONE]) = \rflts \; rs @ [\RONE]$
+ − 434
\item
+ − 435
If $r \neq \RZERO$ and $\nexists rs'. r = \RALTS{rs'}$ then $\rflts \; (rs @ [r])
+ − 436
= (\rflts \; rs) @ [r]$
555
+ − 437
\item
+ − 438
If $r = \RALTS{rs}$ and $r \in rs'$ then for all $r_1 \in rs.
+ − 439
r_1 \in \rflts \; rs'$.
+ − 440
\item
+ − 441
$\rflts \; (rs_a @ \RZERO :: rs_b) = \rflts \; (rs_a @ rs_b)$
543
+ − 442
\end{itemize}
+ − 443
\end{lemma}
+ − 444
\noindent
+ − 445
\begin{proof}
555
+ − 446
By induction on $rs_1$ in the first sub-lemma, and induction on $r$ in the second part,
+ − 447
and induction on $rs$, $rs'$, $rs$, $rs'$, $rs_a$ in the third, fourth, fifth, sixth and
+ − 448
last sub-lemma.
543
+ − 449
\end{proof}
555
+ − 450
554
+ − 451
\subsubsection{The $RL$ Function: Language Interpretation of $\textit{Rrexp}$s}
+ − 452
Much like the definition of $L$ on plain regular expressions, one could also
+ − 453
define the language interpretation of $\rrexp$s.
+ − 454
\begin{center}
+ − 455
\begin{tabular}{lcl}
+ − 456
$RL \; (\ZERO)$ & $\dn$ & $\phi$\\
+ − 457
$RL \; (\ONE)$ & $\dn$ & $\{[]\}$\\
+ − 458
$RL \; (c)$ & $\dn$ & $\{[c]\}$\\
+ − 459
$RL \; \sum rs$ & $\dn$ & $ \bigcup_{r \in rs} (RL \; r)$\\
+ − 460
$RL \; (r_1 \cdot r_2)$ & $\dn$ & $ RL \; (r_1) @ RL \; (r_2)$\\
+ − 461
$RL \; (r^*)$ & $\dn$ & $ (RL(r))^*$
+ − 462
\end{tabular}
+ − 463
\end{center}
+ − 464
\noindent
+ − 465
The main use of $RL$ is to establish some connections between $\rsimp{}$
+ − 466
and $\rnullable{}$:
+ − 467
\begin{lemma}
+ − 468
The following properties hold:
+ − 469
\begin{itemize}
+ − 470
\item
+ − 471
If $\rnullable{r}$, then $\rsimp{r} \neq \RZERO$.
+ − 472
\item
+ − 473
$\rnullable{r \backslash s} \quad $ if and only if $\quad \rnullable{\rderssimp{r}{s}}$.
+ − 474
\end{itemize}
+ − 475
\end{lemma}
+ − 476
\begin{proof}
+ − 477
The first part is by induction on $r$.
+ − 478
The second part is true because property
+ − 479
\[ RL \; r = RL \; (\rsimp{r})\] holds.
+ − 480
\end{proof}
+ − 481
+ − 482
\subsubsection{$\rsimp{}$ is Non-Increasing}
+ − 483
In this subsection, we prove that the function $\rsimp{}$ does not
+ − 484
make the $\llbracket \rrbracket_r$ size increase.
543
+ − 485
+ − 486
554
+ − 487
\begin{lemma}\label{rsimpSize}
+ − 488
$\llbracket \rsimp{r} \rrbracket_r \leq \llbracket r \rrbracket_r$
+ − 489
\end{lemma}
+ − 490
\subsubsection{Simplified $\textit{Rrexp}$s are Good}
+ − 491
We formalise the notion of ``good" regular expressions,
+ − 492
which means regular expressions that
+ − 493
are not fully simplified. For alternative regular expressions that means they
+ − 494
do not contain any nested alternatives like
+ − 495
\[ r_1 + (r_2 + r_3) \], un-removed $\RZERO$s like \[\RZERO + r\]
+ − 496
or duplicate elements in a children regular expression list like \[ \sum [r, r, \ldots]\]:
+ − 497
\begin{center}
+ − 498
\begin{tabular}{@{}lcl@{}}
+ − 499
$\good\; \RZERO$ & $\dn$ & $\textit{false}$\\
+ − 500
$\good\; \RONE$ & $\dn$ & $\textit{true}$\\
+ − 501
$\good\; \RCHAR{c}$ & $\dn$ & $\btrue$\\
+ − 502
$\good\; \RALTS{[]}$ & $\dn$ & $\bfalse$\\
+ − 503
$\good\; \RALTS{[r]}$ & $\dn$ & $\bfalse$\\
+ − 504
$\good\; \RALTS{r_1 :: r_2 :: rs}$ & $\dn$ &
+ − 505
$\textit{isDistinct} \; (r_1 :: r_2 :: rs) \;$\\
+ − 506
& & $\textit{and}\; (\forall r' \in (r_1 :: r_2 :: rs).\; \good \; r'\; \, \textit{and}\; \, \textit{nonAlt}\; r')$\\
+ − 507
$\good \; \RSEQ{\RZERO}{r}$ & $\dn$ & $\bfalse$\\
+ − 508
$\good \; \RSEQ{\RONE}{r}$ & $\dn$ & $\bfalse$\\
+ − 509
$\good \; \RSEQ{r}{\RZERO}$ & $\dn$ & $\bfalse$\\
+ − 510
$\good \; \RSEQ{r_1}{r_2}$ & $\dn$ & $\good \; r_1 \;\, \textit{and} \;\, \good \; r_2$\\
+ − 511
$\good \; \RSTAR{r}$ & $\dn$ & $\btrue$\\
+ − 512
\end{tabular}
+ − 513
\end{center}
+ − 514
\noindent
+ − 515
The predicate $\textit{nonAlt}$ evaluates to true when the regular expression is not an
+ − 516
alternative, and false otherwise.
+ − 517
The $\good$ property is preserved under $\rsimp_{ALTS}$, provided that
+ − 518
its non-empty argument list of expressions are all good themsleves, and $\textit{nonAlt}$,
+ − 519
and unique:
+ − 520
\begin{lemma}\label{rsimpaltsGood}
+ − 521
If $rs \neq []$ and forall $r \in rs. \textit{nonAlt} \; r$ and $\textit{isDistinct} \; rs$,
+ − 522
then $\good \; (\rsimpalts \; rs)$ if and only if forall $r \in rs. \; \good \; r$.
+ − 523
\end{lemma}
+ − 524
\noindent
+ − 525
We also note that
+ − 526
if a regular expression $r$ is good, then $\rflts$ on the singleton
+ − 527
list $[r]$ will not break goodness:
+ − 528
\begin{lemma}\label{flts2}
+ − 529
If $\good \; r$, then forall $r' \in \rflts \; [r]. \; \good \; r'$ and $\textit{nonAlt} \; r'$.
+ − 530
\end{lemma}
+ − 531
\begin{proof}
+ − 532
By an induction on $r$.
+ − 533
\end{proof}
543
+ − 534
\noindent
554
+ − 535
The other observation we make about $\rsimp{r}$ is that it never
+ − 536
comes with nested alternatives, which we describe as the $\nonnested$
+ − 537
property:
+ − 538
\begin{center}
+ − 539
\begin{tabular}{lcl}
+ − 540
$\nonnested \; \, \sum []$ & $\dn$ & $\btrue$\\
+ − 541
$\nonnested \; \, \sum ((\sum rs_1) :: rs_2)$ & $\dn$ & $\bfalse$\\
+ − 542
$\nonnested \; \, \sum (r :: rs)$ & $\dn$ & $\nonnested (\sum rs)$\\
+ − 543
$\nonnested \; \, r $ & $\dn$ & $\btrue$
+ − 544
\end{tabular}
+ − 545
\end{center}
+ − 546
\noindent
+ − 547
The $\rflts$ function
+ − 548
always opens up nested alternatives,
+ − 549
which enables $\rsimp$ to be non-nested:
+ − 550
+ − 551
\begin{lemma}\label{nonnestedRsimp}
+ − 552
$\nonnested \; (\rsimp{r})$
+ − 553
\end{lemma}
+ − 554
\begin{proof}
+ − 555
By an induction on $r$.
+ − 556
\end{proof}
+ − 557
\noindent
+ − 558
With this we could prove that a regular expressions
+ − 559
after simplification and flattening and de-duplication,
+ − 560
will not contain any alternative regular expression directly:
+ − 561
\begin{lemma}\label{nonaltFltsRd}
+ − 562
If $x \in \rdistinct{\rflts\; (\map \; \rsimp{} \; rs)}{\varnothing}$
+ − 563
then $\textit{nonAlt} \; x$.
+ − 564
\end{lemma}
+ − 565
\begin{proof}
+ − 566
By \ref{nonnestedRsimp}.
+ − 567
\end{proof}
+ − 568
\noindent
+ − 569
The other thing we know is that once $\rsimp{}$ had finished
+ − 570
processing an alternative regular expression, it will not
+ − 571
contain any $\RZERO$s, this is because all the recursive
+ − 572
calls to the simplification on the children regular expressions
+ − 573
make the children good, and $\rflts$ will not take out
+ − 574
any $\RZERO$s out of a good regular expression list,
+ − 575
and $\rdistinct{}$ will not mess with the result.
+ − 576
\begin{lemma}\label{flts3Obv}
+ − 577
The following are true:
+ − 578
\begin{itemize}
+ − 579
\item
+ − 580
If for all $r \in rs. \, \good \; r $ or $r = \RZERO$,
+ − 581
then for all $r \in \rflts\; rs. \, \good \; r$.
+ − 582
\item
+ − 583
If $x \in \rdistinct{\rflts\; (\map \; rsimp{}\; rs)}{\varnothing}$
+ − 584
and for all $y$ such that $\llbracket y \rrbracket_r$ less than
+ − 585
$\llbracket rs \rrbracket_r + 1$, either
+ − 586
$\good \; (\rsimp{y})$ or $\rsimp{y} = \RZERO$,
+ − 587
then $\good \; x$.
+ − 588
\end{itemize}
+ − 589
\end{lemma}
+ − 590
\begin{proof}
+ − 591
The first part is by induction on $rs$, where the induction
+ − 592
rule is the inductive cases for $\rflts$.
+ − 593
The second part is a corollary from the first part.
+ − 594
\end{proof}
543
+ − 595
554
+ − 596
And this leads to good structural property of $\rsimp{}$,
+ − 597
that after simplification, a regular expression is
+ − 598
either good or $\RZERO$:
+ − 599
\begin{lemma}\label{good1}
+ − 600
For any r-regular expression $r$, $\good \; \rsimp{r}$ or $\rsimp{r} = \RZERO$.
+ − 601
\end{lemma}
+ − 602
\begin{proof}
+ − 603
By an induction on $r$. The inductive measure is the size $\llbracket \rrbracket_r$.
+ − 604
Lemma \ref{rsimpSize} says that
+ − 605
$\llbracket \rsimp{r}\rrbracket_r$ is smaller than or equal to
+ − 606
$\llbracket r \rrbracket_r$.
+ − 607
Therefore, in the $r_1 \cdot r_2$ and $\sum rs$ case,
+ − 608
Inductive hypothesis applies to the children regular expressions
+ − 609
$r_1$, $r_2$, etc. The lemma \ref{flts3Obv}'s precondition is satisfied
+ − 610
by that as well.
+ − 611
The lemmas \ref{nonnestedRsimp} and \ref{nonaltFltsRd} are used
+ − 612
to ensure that goodness is preserved at the topmost level.
+ − 613
\end{proof}
+ − 614
We shall prove that any good regular expression is
+ − 615
a fixed-point for $\rsimp{}$.
+ − 616
First we prove an auxiliary lemma:
+ − 617
\begin{lemma}\label{goodaltsNonalt}
+ − 618
If $\good \; \sum rs$, then $\rflts\; rs = rs$.
+ − 619
\end{lemma}
+ − 620
\begin{proof}
+ − 621
By an induction on $\sum rs$. The inductive rules are the cases
+ − 622
for $\good$.
+ − 623
\end{proof}
+ − 624
\noindent
+ − 625
Now we are ready to prove that good regular expressions are invariant
+ − 626
of $\rsimp{}$ application:
+ − 627
\begin{lemma}\label{test}
+ − 628
If $\good \;r$ then $\rsimp{r} = r$.
+ − 629
\end{lemma}
+ − 630
\begin{proof}
+ − 631
By an induction on the inductive cases of $\good$.
+ − 632
The lemma \ref{goodaltsNonalt} is used in the alternative
+ − 633
case where 2 or more elements are present in the list.
+ − 634
\end{proof}
555
+ − 635
\noindent
+ − 636
Given below is a property involving $\rflts$, $\rdistinct{}{}$, $\rsimp{}$ and $\rsimp_{ALTS}$,
+ − 637
which requires $\ref{good1}$ to go through smoothly.
+ − 638
It says that an application of $\rsimp_{ALTS}$ can be "absorbed",
+ − 639
if it its output is concatenated with a list and then applied to $\rflts$.
+ − 640
\begin{lemma}\label{flattenRsimpalts}
+ − 641
$\rflts \; ( (\rsimp_{ALTS} \;
+ − 642
(\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing})) ::
+ − 643
\map \; \rsimp{} \; rs' ) =
+ − 644
\rflts \; ( (\rdistinct{(\rflts \; (\map \; \rsimp{}\; rs))}{\varnothing}) @ (
+ − 645
\map \; \rsimp{rs'}))$
554
+ − 646
555
+ − 647
+ − 648
\end{lemma}
+ − 649
\begin{proof}
+ − 650
By \ref{good1}.
+ − 651
\end{proof}
+ − 652
\noindent
+ − 653
+ − 654
+ − 655
+ − 656
+ − 657
+ − 658
We are also
554
+ − 659
\subsubsection{$\rsimp$ is Idempotent}
+ − 660
The idempotency of $\rsimp$ is very useful in
+ − 661
manipulating regular expression terms into desired
+ − 662
forms so that key steps allowing further rewriting to closed forms
+ − 663
are possible.
+ − 664
\begin{lemma}\label{rsimpIdem}
+ − 665
$\rsimp{r} = \rsimp{\rsimp{r}}$
+ − 666
\end{lemma}
+ − 667
+ − 668
\begin{proof}
+ − 669
By \ref{test} and \ref{good1}.
+ − 670
\end{proof}
+ − 671
\noindent
+ − 672
This property means we do not have to repeatedly
+ − 673
apply simplification in each step, which justifies
+ − 674
our definition of $\blexersimp$.
+ − 675
532
+ − 676
554
+ − 677
On the other hand, we could repeat the same $\rsimp{}$ applications
+ − 678
on regular expressions as many times as we want, if we have at least
+ − 679
one simplification applied to it, and apply it wherever we would like to:
+ − 680
\begin{corollary}\label{headOneMoreSimp}
555
+ − 681
The following properties hold, directly from \ref{rsimpIdem}:
+ − 682
+ − 683
\begin{itemize}
+ − 684
\item
+ − 685
$\map \; \rsimp{(r :: rs)} = \map \; \rsimp{} \; (\rsimp{r} :: rs)$
+ − 686
\item
+ − 687
$\rsimp{(\RALTS{rs})} = \rsimp{(\RALTS{\map \; \rsimp{} \; rs})}$
+ − 688
\end{itemize}
554
+ − 689
\end{corollary}
+ − 690
\noindent
+ − 691
This will be useful in later closed form proof's rewriting steps.
+ − 692
Similarly, we point out the following useful facts below:
+ − 693
\begin{lemma}
+ − 694
The following equalities hold if $r = \rsimp{r'}$ for some $r'$:
+ − 695
\begin{itemize}
+ − 696
\item
+ − 697
If $r = \sum rs$ then $\rsimpalts \; rs = \sum rs$.
+ − 698
\item
+ − 699
If $r = \sum rs$ then $\rdistinct{rs}{\varnothing} = rs$.
+ − 700
\item
+ − 701
$\rsimpalts \; (\rdistinct{\rflts \; [r]}{\varnothing}) = r$.
+ − 702
\end{itemize}
+ − 703
\end{lemma}
+ − 704
\begin{proof}
+ − 705
By application of \ref{rsimpIdem} and \ref{good1}.
+ − 706
\end{proof}
+ − 707
+ − 708
\noindent
+ − 709
With the idempotency of $\rsimp{}$ and its corollaries,
+ − 710
we can start proving some key equalities leading to the
+ − 711
closed forms.
+ − 712
Now presented are a few equivalent terms under $\rsimp{}$.
+ − 713
We use $r_1 \sequal r_2 $ here to denote $\rsimp{r_1} = \rsimp{r_2}$.
+ − 714
\begin{lemma}
+ − 715
\begin{itemize}
555
+ − 716
The following equivalence hold:
554
+ − 717
\item
+ − 718
$\rsimpalts \; (\RZERO :: rs) \sequal \rsimpalts\; rs$
+ − 719
\item
+ − 720
$\rsimpalts \; rs \sequal \rsimpalts (\map \; \rsimp{} \; rs)$
+ − 721
\item
+ − 722
$\RALTS{\RALTS{rs}} \sequal \RALTS{rs}$
555
+ − 723
\item
+ − 724
$\sum ((\sum rs_a) :: rs_b) \sequal \sum rs_a @ rs_b$
+ − 725
\item
+ − 726
$\RALTS{rs} = \RALTS{\map \; \rsimp{} \; rs}$
554
+ − 727
\end{itemize}
+ − 728
\end{lemma}
555
+ − 729
\begin{proof}
+ − 730
By induction on the lists involved.
+ − 731
\end{proof}
+ − 732
\noindent
+ − 733
Similarly,
+ − 734
we introduce the equality for $\sum$ when certain child regular expressions
+ − 735
are $\sum$ themselves:
+ − 736
\begin{lemma}\label{simpFlatten3}
+ − 737
One can flatten the inside $\sum$ of a $\sum$ if it is being
+ − 738
simplified. Concretely,
+ − 739
\begin{itemize}
+ − 740
\item
+ − 741
If for all $r \in rs, rs', rs''$, we have $\good \; r $
+ − 742
or $r = \RZERO$, then $\sum (rs' @ rs @ rs'') \sequal
+ − 743
\sum (rs' @ [\sum rs] @ rs'')$ holds. As a corollary,
+ − 744
\item
+ − 745
$\sum (rs' @ [\sum rs] @ rs'') \sequal \sum (rs' @ rs @ rs'')$
+ − 746
\end{itemize}
+ − 747
\end{lemma}
+ − 748
\begin{proof}
+ − 749
By rewriting steps involving the use of \ref{test} and \ref{rdistinctConcatGeneral}.
+ − 750
The second sub-lemma is a corollary of the previous.
+ − 751
\end{proof}
+ − 752
%Rewriting steps not put in--too long and complicated-------------------------------
+ − 753
\begin{comment}
+ − 754
\begin{center}
+ − 755
$\rsimp{\sum (rs' @ rs @ rs'')} \stackrel{def of bsimp}{=}$ \\
+ − 756
$\rsimpalts \; (\rdistinct{\rflts \; ((\map \; \rsimp{}\; rs') @ (\map \; \rsimp{} \; rs ) @ (\map \; \rsimp{} \; rs''))}{\varnothing})$ \\
+ − 757
$\stackrel{by \ref{test}}{=}
+ − 758
\rsimpalts \; (\rdistinct{(\rflts \; rs' @ \rflts \; rs @ \rflts \; rs'')}{
+ − 759
\varnothing})$\\
+ − 760
$\stackrel{by \ref{rdistinctConcatGeneral}}{=}
+ − 761
\rsimpalts \; (\rdistinct{\rflts \; rs'}{\varnothing} @ \rdistinct{(
+ − 762
\rflts\; rs @ \rflts \; rs'')}{\rflts \; rs'})$\\
+ − 763
+ − 764
\end{center}
+ − 765
\end{comment}
+ − 766
%Rewriting steps not put in--too long and complicated-------------------------------
554
+ − 767
\noindent
+ − 768
We need more equalities like the above to enable a closed form,
+ − 769
but to proceed we need to introduce two rewrite relations,
+ − 770
to make things smoother.
557
+ − 771
\subsubsection{The rewrite relation $\hrewrite$ , $\scfrewrites$ , $\frewrite$ and $\grewrite$}
554
+ − 772
Insired by the success we had in the correctness proof
+ − 773
in \ref{Bitcoded2}, where we invented
555
+ − 774
a term rewriting system to capture the similarity between terms,
+ − 775
we follow suit here defining simplification
+ − 776
steps as rewriting steps. This allows capturing
+ − 777
similarities between terms that would be otherwise
+ − 778
hard to express.
+ − 779
557
+ − 780
We use $\hrewrite$ for one-step atomic rewrite of
+ − 781
regular expression simplification,
555
+ − 782
$\frewrite$ for rewrite of list of regular expressions that
+ − 783
include all operations carried out in $\rflts$, and $\grewrite$ for
+ − 784
rewriting a list of regular expressions possible in both $\rflts$ and $\rdistinct{}{}$.
+ − 785
Their reflexive transitive closures are used to denote zero or many steps,
+ − 786
as was the case in the previous chapter.
554
+ − 787
The presentation will be more concise than that in \ref{Bitcoded2}.
+ − 788
To differentiate between the rewriting steps for annotated regular expressions
+ − 789
and $\rrexp$s, we add characters $h$ and $g$ below the squig arrow symbol
+ − 790
to mean atomic simplification transitions
+ − 791
of $\rrexp$s and $\rrexp$ lists, respectively.
+ − 792
555
+ − 793
+ − 794
+ − 795
List of one-step rewrite rules for $\rrexp$ ($\hrewrite$):
+ − 796
+ − 797
554
+ − 798
\begin{center}
555
+ − 799
\begin{mathpar}
+ − 800
\inferrule[RSEQ0L]{}{\RZERO \cdot r_2 \hrewrite \RZERO\\}
+ − 801
+ − 802
\inferrule[RSEQ0R]{}{r_1 \cdot \RZERO \hrewrite \RZERO\\}
+ − 803
+ − 804
\inferrule[RSEQ1]{}{(\RONE \cdot r) \hrewrite r\\}\\
+ − 805
+ − 806
\inferrule[RSEQL]{ r_1 \hrewrite r_2}{r_1 \cdot r_3 \hrewrite r_2 \cdot r_3\\}
+ − 807
+ − 808
\inferrule[RSEQR]{ r_3 \hrewrite r_4}{r_1 \cdot r_3 \hrewrite r_1 \cdot r_4\\}\\
+ − 809
+ − 810
\inferrule[RALTSChild]{r \hrewrite r'}{\sum (rs_1 @ [r] @ rs_2) \hrewrite \sum (rs_1 @ [r'] @ rs_2)\\}
+ − 811
+ − 812
\inferrule[RALTS0]{}{\sum (rs_a @ [\RZERO] @ rs_b) \hrewrite \sum (rs_a @ rs_b)}
+ − 813
+ − 814
\inferrule[RALTSNested]{}{\sum (rs_a @ [\sum rs_1] @ rs_b) \hrewrite \sum (rs_a @ rs_1 @ rs_b)}
+ − 815
+ − 816
\inferrule[RALTSNil]{}{ \sum [] \hrewrite \RZERO\\}
+ − 817
+ − 818
\inferrule[RALTSSingle]{}{ \sum [r] \hrewrite r\\}
+ − 819
+ − 820
\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}
+ − 821
+ − 822
\end{mathpar}
+ − 823
\end{center}
554
+ − 824
557
+ − 825
+ − 826
List of rewrite rules for a list of regular expressions,
+ − 827
where each element can rewrite in many steps to the other (scf stands for
+ − 828
li\emph{s}t \emph{c}losed \emph{f}orm). This relation is similar to the
+ − 829
$\stackrel{s*}{\rightsquigarrow}$ for annotated regular expressions.
+ − 830
+ − 831
\begin{center}
+ − 832
\begin{mathpar}
+ − 833
\inferrule{}{[] \scfrewrites [] }
+ − 834
\inferrule{r \hrewrites r' \\ rs \scfrewrites rs'}{r :: rs \scfrewrites r' :: rs'}
+ − 835
\end{mathpar}
+ − 836
\end{center}
555
+ − 837
%frewrite
+ − 838
List of one-step rewrite rules for flattening
+ − 839
a list of regular expressions($\frewrite$):
+ − 840
\begin{center}
+ − 841
\begin{mathpar}
+ − 842
\inferrule{}{\RZERO :: rs \frewrite rs \\}
+ − 843
+ − 844
\inferrule{}{(\sum rs) :: rs_a \frewrite rs @ rs_a \\}
+ − 845
+ − 846
\inferrule{rs_1 \frewrite rs_2}{r :: rs_1 \frewrite r :: rs_2}
+ − 847
\end{mathpar}
+ − 848
\end{center}
+ − 849
+ − 850
Lists of one-step rewrite rules for flattening and de-duplicating
+ − 851
a list of regular expressions ($\grewrite$):
+ − 852
\begin{center}
+ − 853
\begin{mathpar}
557
+ − 854
\inferrule{}{\RZERO :: rs \grewrite rs \\}
532
+ − 855
557
+ − 856
\inferrule{}{(\sum rs) :: rs_a \grewrite rs @ rs_a \\}
555
+ − 857
557
+ − 858
\inferrule{rs_1 \grewrite rs_2}{r :: rs_1 \grewrite r :: rs_2}
555
+ − 859
+ − 860
\inferrule[dB]{}{rs_a @ [a] @ rs_b @[a] @ rs_c \grewrite rs_a @ [a] @ rsb @ rsc}
+ − 861
\end{mathpar}
+ − 862
\end{center}
+ − 863
+ − 864
\noindent
+ − 865
The reason why we take the trouble of defining
+ − 866
two separate list rewriting definitions $\frewrite$ and $\grewrite$
557
+ − 867
is to separate the two stages of simplification: flattening and de-duplicating.
+ − 868
Sometimes $\grewrites$ is slightly too powerful
+ − 869
so we would rather use $\frewrites$ which makes certain rewriting steps
+ − 870
more straightforward to prove.
556
+ − 871
For example, when proving the closed-form for the alternative regular expression,
+ − 872
one of the rewriting steps would be:
+ − 873
\begin{lemma}
557
+ − 874
$\sum (\rDistinct \;\; (\map \; (\_ \backslash x) \; (\rflts \; rs)) \;\; \varnothing) \sequal
+ − 875
\sum (\rDistinct \;\; (\rflts \; (\map \; (\_ \backslash x) \; rs)) \;\; \varnothing)
556
+ − 876
$
+ − 877
\end{lemma}
+ − 878
\noindent
+ − 879
Proving this is by first showing
557
+ − 880
\begin{lemma}\label{earlyLaterDerFrewrites}
556
+ − 881
$\map \; (\_ \backslash x) \; (\rflts \; rs) \frewrites
557
+ − 882
\rflts \; (\map \; (\_ \backslash x) \; rs)$
556
+ − 883
\end{lemma}
+ − 884
\noindent
+ − 885
and then using lemma
+ − 886
\begin{lemma}\label{frewritesSimpeq}
+ − 887
If $rs_1 \frewrites rs_2 $, then $\sum (\rDistinct \; rs_1 \; \varnothing) \sequal
557
+ − 888
\sum (\rDistinct \; rs_2 \; \varnothing)$.
556
+ − 889
\end{lemma}
557
+ − 890
\noindent
+ − 891
is a piece of cake.
+ − 892
But this trick will not work for $\grewrites$.
+ − 893
For example, a rewriting step in proving
+ − 894
closed forms is:
+ − 895
\begin{center}
+ − 896
$\rsimp{(\rsimpalts \; (\map \; (\_ \backslash x) \; (\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs}))))}{\varnothing})))}$\\
+ − 897
$=$ \\
+ − 898
$\rsimp{(\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \; (\rflts \; (\map \; (\rsimp{} \; \circ \; (\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}))} $
+ − 899
\noindent
+ − 900
\end{center}
+ − 901
For this one would hope to have a rewriting relation between the two lists involved,
+ − 902
similar to \ref{earlyLaterDerFrewrites}. However, it turns out that
556
+ − 903
\begin{center}
+ − 904
$\map \; (\_ \backslash x) \; (\rDistinct \; rs \; rset) \grewrites \rDistinct \; (\map \;
557
+ − 905
(\_ \backslash x) \; rs) \; ( rset \backslash x)$
556
+ − 906
\end{center}
+ − 907
\noindent
557
+ − 908
does $\mathbf{not}$ hold in general.
+ − 909
For this rewriting step we will introduce some slightly more cumbersome
+ − 910
proof technique in later sections.
+ − 911
The point is that $\frewrite$
+ − 912
allows us to prove equivalence in a straightforward two-step method that is
+ − 913
not possible for $\grewrite$, thereby reducing the complexity of the entire proof.
555
+ − 914
556
+ − 915
557
+ − 916
\subsubsection{Terms That Can Be Rewritten Using $\hrewrites$, $\grewrites$, and $\frewrites$}
+ − 917
We present in the below lemma a few pairs of terms that are rewritable via
+ − 918
$\grewrites$:
+ − 919
\begin{lemma}\label{gstarRdistinctGeneral}
+ − 920
\begin{itemize}
+ − 921
\item
+ − 922
$rs_1 @ rs \grewrites rs_1 @ (\rDistinct \; rs \; rs_1)$
+ − 923
\item
+ − 924
$rs \grewrites \rDistinct \; rs \; \varnothing$
+ − 925
\item
+ − 926
$rs_a @ (\rDistinct \; rs \; rs_a) \grewrites rs_a @ (\rDistinct \;
+ − 927
rs \; (\{\RZERO\} \cup rs_a))$
+ − 928
\item
+ − 929
$rs \;\; @ \;\; \rDistinct \; rs_a \; rset \grewrites rs @ \rDistinct \; rs_a \;
+ − 930
(rest \cup rs)$
+ − 931
+ − 932
\end{itemize}
+ − 933
\end{lemma}
+ − 934
\noindent
+ − 935
If a pair of terms $rs_1, rs_2$ are rewritable via $\grewrites$ to each other,
+ − 936
then they are equivalent under $\rsimp{}$:
+ − 937
\begin{lemma}\label{grewritesSimpalts}
+ − 938
If $rs_1 \grewrites rs_2$, then
+ − 939
we have the following equivalence hold:
+ − 940
\begin{itemize}
+ − 941
\item
+ − 942
$\sum rs_1 \sequal \sum rs_2$
+ − 943
\item
+ − 944
$\rsimpalts \; rs_1 \sequal \rsimpalts \; rs_2$
+ − 945
\end{itemize}
+ − 946
\end{lemma}
+ − 947
\noindent
+ − 948
Here are a few connecting lemmas showing that
+ − 949
if a list of regular expressions can be rewritten using $\grewrites$ or $\frewrites $ or
+ − 950
$\scfrewrites$,
+ − 951
then an alternative constructor taking the list can also be rewritten using $\hrewrites$:
+ − 952
\begin{lemma}
+ − 953
\begin{itemize}
+ − 954
\item
+ − 955
If $rs \grewrites rs'$ then $\sum rs \hrewrites \sum rs'$.
+ − 956
\item
+ − 957
If $rs \grewrites rs'$ then $\sum rs \hrewrites \rsimpalts \; rs'$
+ − 958
\item
+ − 959
If $rs_1 \scfrewrites rs_2$ then $\sum (rs @ rs_1) \hrewrites \sum (rs @ rs_2)$
+ − 960
\item
+ − 961
If $rs_1 \scfrewrites rs_2$ then $\sum rs_1 \hrewrites \sum rs_2$
+ − 962
+ − 963
\end{itemize}
+ − 964
\end{lemma}
+ − 965
\noindent
+ − 966
Here comes the meat of the proof,
+ − 967
which says that once two lists are rewritable to each other,
+ − 968
then they are equivalent under $\rsimp{}$:
+ − 969
\begin{lemma}
+ − 970
If $r_1 \hrewrites r_2$ then $r_1 \sequal r_2$.
+ − 971
\end{lemma}
+ − 972
+ − 973
\noindent
+ − 974
And similar to \ref{Bitcoded2} one can preserve rewritability after taking derivative
+ − 975
of two regular expressions on both sides:
+ − 976
\begin{lemma}\label{interleave}
+ − 977
If $r \hrewrites r' $ then $\rder{c}{r} \hrewrites \rder{c}{r'}$
+ − 978
\end{lemma}
+ − 979
\noindent
+ − 980
This allows proving more $\mathbf{rsimp}$-equivalent terms, involving $\backslash_r$ now.
+ − 981
\begin{lemma}\label{insideSimpRemoval}
+ − 982
$\rsimp{\rder{c}{\rsimp{r}}} = \rsimp{\rder{c}{r}} $
+ − 983
\end{lemma}
+ − 984
\noindent
+ − 985
\begin{proof}
+ − 986
By \ref{interleave} and \ref{rsimpIdem}.
+ − 987
\end{proof}
+ − 988
\noindent
+ − 989
And this unlocks more equivalent terms:
+ − 990
\begin{lemma}\label{Simpders}
+ − 991
As corollaries of \ref{insideSimpRemoval}, we have
+ − 992
\begin{itemize}
+ − 993
\item
+ − 994
If $s \neq []$ then $\rderssimp{r}{s} = \rsimp{(\rders \; r \; s)}$.
+ − 995
\item
+ − 996
$\rsimpalts \; (\map \; (\_ \backslash_r x) \;
+ − 997
(\rdistinct{rs}{\varnothing})) \sequal
+ − 998
\rsimpalts \; (\rDistinct \;
+ − 999
(\map \; (\_ \backslash_r x) rs) \;\varnothing )$
+ − 1000
\end{itemize}
+ − 1001
\end{lemma}
+ − 1002
\noindent
+ − 1003
+ − 1004
Finally,
+ − 1005
together with
+ − 1006
\begin{lemma}\label{rderRsimpAltsCommute}
+ − 1007
$\rder{x}{(\rsimpalts \; rs)} = \rsimpalts \; (\map \; (\rder{x}{\_}) \; rs)$
+ − 1008
\end{lemma}
+ − 1009
\noindent
+ − 1010
this leads to the first closed form--
+ − 1011
\begin{lemma}\label{altsClosedForm}
556
+ − 1012
\begin{center}
557
+ − 1013
$\rderssimp{(\sum rs)}{s} \sequal
+ − 1014
\sum \; (\map \; (\rderssimp{\_}{s}) \; rs)$
556
+ − 1015
\end{center}
557
+ − 1016
\end{lemma}
556
+ − 1017
+ − 1018
\noindent
557
+ − 1019
\begin{proof}
+ − 1020
By a reverse induction on the string $s$.
+ − 1021
One rewriting step, as we mentioned earlier,
+ − 1022
involves
+ − 1023
\begin{center}
+ − 1024
$\rsimpalts \; (\map \; (\_ \backslash x) \;
+ − 1025
(\rdistinct{(\rflts \; (\map \; (\rsimp{} \; \circ \;
+ − 1026
(\lambda r. \rderssimp{r}{xs}))))}{\varnothing}))
+ − 1027
\sequal
+ − 1028
\rsimpalts \; (\rdistinct{(\map \; (\_ \backslash x) \;
+ − 1029
(\rflts \; (\map \; (\rsimp{} \; \circ \;
+ − 1030
(\lambda r. \rderssimp{r}{xs})))) ) }{\varnothing}) $.
+ − 1031
\end{center}
+ − 1032
This can be proven by a combination of
+ − 1033
\ref{grewritesSimpalts}, \ref{gstarRdistinctGeneral}, \ref{rderRsimpAltsCommute}, and
+ − 1034
\ref{insideSimpRemoval}.
+ − 1035
\end{proof}
+ − 1036
\noindent
+ − 1037
This closed form has a variant which can be more convenient in later proofs:
+ − 1038
\begin{corollary}
+ − 1039
If $s \neq []$ then
+ − 1040
$\rderssimp \; (\sum \; rs) \; s =
+ − 1041
\rsimp{(\sum \; (\map \; \rderssimp{\_}{s} \; rs))}$.
+ − 1042
\end{corollary}
+ − 1043
\noindent
+ − 1044
The harder closed forms are the sequence and star ones.
+ − 1045
Before we go on to obtain them, some preliminary definitions
+ − 1046
are needed to make proof statements concise.
556
+ − 1047
557
+ − 1048
\section{"Closed Forms" of regular expressions' derivatives w.r.t strings}
+ − 1049
We note that the different ways in which regular expressions are
+ − 1050
nested do not matter under $\rsimp{}$:
+ − 1051
\begin{center}
+ − 1052
To be proven:\\
+ − 1053
$\rsimp{r} \stackrel{?}{=} \rsimp{r'}$ if $r = \sum [r_1, r_2, r_3, \ldots]$
+ − 1054
and $r' =(\ldots ((r_1 + r_2) + r_3) + \ldots)$
+ − 1055
\end{center}
+ − 1056
\noindent
+ − 1057
This intuition is also echoed by IndianPaper, where they gave
+ − 1058
a pencil-and-paper derivation of $(r_1 \cdot r_2)\backslash s$:
532
+ − 1059
\begin{center}
557
+ − 1060
$((r_1 \cdot r_2) \backslash_r c_1) \stackrel{\backslash_r c_2}{=}
+ − 1061
((r_1 \backslash_r c_1) \cdot r_2 + (\delta\; (\rnullable \; r_1),\; r_2 \backslash_r c_1))
+ − 1062
\stackrel{\backslash_r c_2}{=}
+ − 1063
((r_1 \backslash_r c_1c_2 \cdot r_2 + (\delta \; (\rnullable) \; r_1, r_2 \backslash_r c_1c_2)
+ − 1064
+ (\delta \; (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)
+ − 1065
$
+ − 1066
\end{center}
+ − 1067
\noindent
+ − 1068
The $\delta \; b \; r$ function above turns the entire term into $\RZERO$
+ − 1069
if the boolean condition $b$ evaluates to false, and outputs $r$ otherwise.
+ − 1070
The equality in their derivation steps should be interpretated
+ − 1071
as language equivalence. To pin down the intuition into rigorous terms,
+ − 1072
$\sflat{}$ is used to enable the transformation from
+ − 1073
a left-associative nested sequence of alternatives into
+ − 1074
a flattened list:
+ − 1075
$r = \sum [r_1, r_2, r_3, \ldots] \stackrel{\sflat{}}{\rightarrow}
+ − 1076
(\ldots ((r_1 + r_2) + r_3) + \ldots)$
+ − 1077
The definitions $\sflat{}$, $\sflataux{}$ are given below.
+ − 1078
+ − 1079
\begin{center}
+ − 1080
\begin{tabular}{ccc}
+ − 1081
$\sflataux{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
+ − 1082
$\sflataux{\AALTS{ }{[]}}$ & $ = $ & $ []$\\
+ − 1083
$\sflataux r$ & $=$ & $ [r]$
532
+ − 1084
\end{tabular}
+ − 1085
\end{center}
+ − 1086
557
+ − 1087
\begin{center}
+ − 1088
\begin{tabular}{ccc}
+ − 1089
$\sflat{(\sum r :: rs)}$ & $=$ & $\sum (\sflataux{r} @ rs)$\\
+ − 1090
$\sflat{\sum []}$ & $ = $ & $ \sum []$\\
+ − 1091
$\sflat r$ & $=$ & $ r$
+ − 1092
\end{tabular}
+ − 1093
\end{center}
+ − 1094
The intuition behind $\sflataux{\_}$ is to break up nested regexes
+ − 1095
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
+ − 1096
into a more "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
+ − 1097
It will return the singleton list $[r]$ otherwise.
+ − 1098
$\sflat{\_}$ works the same as $\sflataux{\_}$, except that it keeps
+ − 1099
the output type a regular expression, not a list.
+ − 1100
$\sflataux{\_}$ and $\sflat{\_}$ is only recursive in terms of the
+ − 1101
first element of the list of children of
+ − 1102
an alternative regex ($\AALTS{ }{rs}$), and is purposefully built for dealing with the regular expression
+ − 1103
$r_1 \cdot r_2 \backslash s$.
+ − 1104
\subsection{The $\textit{vsuf}$ Function}
+ − 1105
Note that $((r_1 \backslash_r c_1c_2 \cdot r_2 + (\delta \; (\rnullable) \; r_1, r_2 \backslash_r c_1c_2)
+ − 1106
+ (\delta \; (\rnullable \; r_1 \backslash_r c)\; r_2 \backslash_r c_2)$ does not faithfully
+ − 1107
represent what would the intermediate derivatives would actually look like.
+ − 1108
For example, when $r_1$ and $r_1 \backslash_r c_1$ are nullable,
+ − 1109
the regular expression would not look like $(r_1 \backslash_r c_1c_2 + \RZERO ) + \RZERO$,
+ − 1110
but actually $r_1\backslash_r c_1c_2$, the redundant $\RZERO$s will not be created in the
+ − 1111
first place.
+ − 1112
In a closed-form one would want to take into account this
+ − 1113
and generate the list of string pairs $(s', s'')$ where $s'@s'' = s$ such that
+ − 1114
only $r_1 \backslash s'$ nullable.
+ − 1115
With $\sflat{\_}$ and $\sflataux{\_}$ we make
+ − 1116
precise what "closed forms" we have for the sequence derivatives and their simplifications,
+ − 1117
in other words, how can we construct $(r_1 \cdot r_2) \backslash s$
+ − 1118
and $\bderssimp{(r_1\cdot r_2)}{s}$,
+ − 1119
if we are only allowed to use a combination of $r_1 \backslash s'$ ($\bderssimp{r_1}{s'}$)
+ − 1120
and $r_2 \backslash s'$ ($\bderssimp{r_2}{s'}$), where $s'$ ranges over
+ − 1121
the substring of $s$?
+ − 1122
First let's look at a series of derivatives steps on a sequence
+ − 1123
regular expression, (assuming) that each time the first
+ − 1124
component of the sequence is always nullable):
+ − 1125
\begin{center}
532
+ − 1126
557
+ − 1127
$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$\\
+ − 1128
$((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
+ − 1129
\ldots$
+ − 1130
+ − 1131
\end{center}
+ − 1132
%TODO: cite indian paper
+ − 1133
Indianpaper have come up with a slightly more formal way of putting the above process:
+ − 1134
\begin{center}
+ − 1135
$r_1 \cdot r_2 \backslash (c_1 :: c_2 ::\ldots c_n) \myequiv r_1 \backslash (c_1 :: c_2:: \ldots c_n) +
+ − 1136
\delta(\nullable(r_1 \backslash (c_1 :: c_2 \ldots c_{n-1}) ), r_2 \backslash (c_n)) + \ldots
+ − 1137
+ \delta (\nullable(r_1), r_2\backslash (c_1 :: c_2 ::\ldots c_n))$
+ − 1138
\end{center}
+ − 1139
where $\delta(b, r)$ will produce $r$
+ − 1140
if $b$ evaluates to true,
+ − 1141
and $\ZERO$ otherwise.
+ − 1142
+ − 1143
But the $\myequiv$ sign in the above equation means language equivalence instead of syntactical
+ − 1144
equivalence. To make this intuition useful
+ − 1145
for a formal proof, we need something
+ − 1146
stronger than language equivalence.
+ − 1147
With the help of $\sflat{\_}$ we can state the equation in Indianpaper
+ − 1148
more rigorously:
532
+ − 1149
\begin{lemma}
557
+ − 1150
$\sflat{(r_1 \cdot r_2) \backslash s} = \RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}$
532
+ − 1151
\end{lemma}
+ − 1152
557
+ − 1153
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string:
+ − 1154
+ − 1155
\begin{center}
+ − 1156
\begin{tabular}{lcl}
+ − 1157
$\vsuf{[]}{\_} $ & $=$ & $[]$\\
+ − 1158
$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} (\rnullable{r_1}) \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
+ − 1159
&& $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) }) $
+ − 1160
\end{tabular}
+ − 1161
\end{center}
+ − 1162
It takes into account which prefixes $s'$ of $s$ would make $r \backslash s'$ nullable,
+ − 1163
and keep a list of suffixes $s''$ such that $s' @ s'' = s$. The list is sorted in
+ − 1164
the order $r_2\backslash s''$ appears in $(r_1\cdot r_2)\backslash s$.
+ − 1165
In essence, $\vsuf{\_}{\_}$ is doing a "virtual derivative" of $r_1 \cdot r_2$, but instead of producing
+ − 1166
the entire result of $(r_1 \cdot r_2) \backslash s$,
+ − 1167
it only stores all the $s''$ such that $r_2 \backslash s''$ are occurring terms in $(r_1\cdot r_2)\backslash s$.
+ − 1168
+ − 1169
With this we can also add simplifications to both sides and get
532
+ − 1170
\begin{lemma}
557
+ − 1171
$\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}))}}$
532
+ − 1172
\end{lemma}
557
+ − 1173
Together with the idempotency property of $\rsimp{}$\ref{rsimpIdem},
+ − 1174
%TODO: state the idempotency property of rsimp
+ − 1175
it is possible to convert the above lemma to obtain a "closed form"
+ − 1176
for derivatives nested with simplification:
532
+ − 1177
\begin{lemma}
557
+ − 1178
$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{\AALTS{[[]}{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}}$
532
+ − 1179
\end{lemma}
557
+ − 1180
And now the reason we have (1) in section 1 is clear:
+ − 1181
$\rsize{\rsimp{\RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map \;(r_2 \backslash \_) \; (\vsuf{s}{r_1}))}}}$,
+ − 1182
is equal to $\rsize{\rsimp{\RALTS{ ((r_1 \backslash s) \cdot r_2 :: (\map \; (r_2 \backslash \_) \; (\textit{Suffix}(r1, s))))}}}$
+ − 1183
as $\vsuf{s}{r_1}$ is one way of expressing the list $\textit{Suffix}( r_1, s)$.
532
+ − 1184
557
+ − 1185
%----------------------------------------------------------------------------------------
+ − 1186
% SECTION 3
+ − 1187
%----------------------------------------------------------------------------------------
+ − 1188
+ − 1189
\section{interaction between $\distinctWith$ and $\flts$}
+ − 1190
Note that it is not immediately obvious that
+ − 1191
\begin{center}
+ − 1192
$\llbracket \distinctWith (\flts \textit{rs}) = \phi \rrbracket \leq \llbracket \distinctWith \textit{rs} = \phi \rrbracket $.
+ − 1193
\end{center}
+ − 1194
The intuition is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of
+ − 1195
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$.
+ − 1196
532
+ − 1197
554
+ − 1198
\subsection{A Closed Form for the Sequence Regular Expression}
+ − 1199
\begin{quote}\it
+ − 1200
Claim: For regular expressions $r_1 \cdot r_2$, we claim that
+ − 1201
\begin{center}
+ − 1202
$ \rderssimp{r_1 \cdot r_2}{s} =
+ − 1203
\rsimp{(\sum (r_1 \backslash s \cdot r_2 ) \; :: \;(\map \; \rderssimp{r2}{\_} \;(\vsuf{s}{r_1})))}$
+ − 1204
\end{center}
+ − 1205
\end{quote}
+ − 1206
\noindent
+ − 1207
+ − 1208
Before we get to the proof that says the intermediate result of our lexer will
+ − 1209
remain finitely bounded, which is an important efficiency/liveness guarantee,
+ − 1210
we shall first develop a few preparatory properties and definitions to
+ − 1211
make the process of proving that a breeze.
+ − 1212
+ − 1213
We define rewriting relations for $\rrexp$s, which allows us to do the
+ − 1214
same trick as we did for the correctness proof,
+ − 1215
but this time we will have stronger equalities established.
+ − 1216
532
+ − 1217
553
+ − 1218
\section{Estimating the Closed Forms' sizes}
532
+ − 1219
553
+ − 1220
The closed form $f\; (g\; (\sum rs)) $ is controlled
+ − 1221
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
+ − 1222
%-----------------------------------
+ − 1223
% SECTION 2
+ − 1224
%-----------------------------------
+ − 1225
+ − 1226
\begin{theorem}
+ − 1227
For any regex $r$, $\exists N_r. \forall s. \; \llbracket{\bderssimp{r}{s}}\rrbracket \leq N_r$
+ − 1228
\end{theorem}
+ − 1229
+ − 1230
\noindent
+ − 1231
\begin{proof}
+ − 1232
We prove this by induction on $r$. The base cases for $\AZERO$,
+ − 1233
$\AONE \textit{bs}$ and $\ACHAR \textit{bs} c$ are straightforward. The interesting case is
+ − 1234
for sequences of the form $\ASEQ{bs}{r_1}{r_2}$. In this case our induction
+ − 1235
hypotheses state $\exists N_1. \forall s. \; \llbracket \bderssimp{r}{s} \rrbracket \leq N_1$ and
+ − 1236
$\exists N_2. \forall s. \; \llbracket \bderssimp{r_2}{s} \rrbracket \leq N_2$. We can reason as follows
+ − 1237
%
+ − 1238
\begin{center}
+ − 1239
\begin{tabular}{lcll}
+ − 1240
& & $ \llbracket \bderssimp{\ASEQ{bs}{r_1}{r_2} }{s} \rrbracket$\\
+ − 1241
& $ = $ & $\llbracket bsimp\,(\textit{ALTs}\;bs\;(\ASEQ{nil}{\bderssimp{ r_1}{s}}{ r_2} ::
+ − 1242
[\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]))\rrbracket $ & (1) \\
+ − 1243
& $\leq$ &
+ − 1244
$\llbracket\textit{\distinctWith}\,((\ASEQ{nil}{\bderssimp{r_1}{s}}{r_2}$) ::
+ − 1245
[$\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)])\,\approx\;{}\rrbracket + 1 $ & (2) \\
+ − 1246
& $\leq$ & $\llbracket\ASEQ{bs}{\bderssimp{ r_1}{s}}{r_2}\rrbracket +
+ − 1247
\llbracket\textit{distinctWith}\,[\bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]\,\approx\;{}\rrbracket + 1 $ & (3) \\
+ − 1248
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 +
+ − 1249
\llbracket \distinctWith\,[ \bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)] \,\approx\;\rrbracket$ & (4)\\
+ − 1250
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 + l_{N_{2}} * N_{2}$ & (5)
+ − 1251
\end{tabular}
+ − 1252
\end{center}
+ − 1253
+ − 1254
+ − 1255
\noindent
+ − 1256
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$).
+ − 1257
The reason why we could write the derivatives of a sequence this way is described in section 2.
+ − 1258
The term (2) is used to control (1).
+ − 1259
That is because one can obtain an overall
+ − 1260
smaller regex list
+ − 1261
by flattening it and removing $\ZERO$s first before applying $\distinctWith$ on it.
+ − 1262
Section 3 is dedicated to its proof.
+ − 1263
In (3) we know that $\llbracket \ASEQ{bs}{(\bderssimp{ r_1}{s}}{r_2}\rrbracket$ is
+ − 1264
bounded by $N_1 + \llbracket{}r_2\rrbracket + 1$. In (5) we know the list comprehension contains only regular expressions of size smaller
+ − 1265
than $N_2$. The list length after $\distinctWith$ is bounded by a number, which we call $l_{N_2}$. It stands
+ − 1266
for the number of distinct regular expressions smaller than $N_2$ (there can only be finitely many of them).
+ − 1267
We reason similarly for $\STAR$.\medskip
+ − 1268
\end{proof}
+ − 1269
+ − 1270
What guarantee does this bound give us?
+ − 1271
+ − 1272
Whatever the regex is, it will not grow indefinitely.
+ − 1273
Take our previous example $(a + aa)^*$ as an example:
+ − 1274
\begin{center}
+ − 1275
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
+ − 1276
\begin{tikzpicture}
+ − 1277
\begin{axis}[
+ − 1278
xlabel={number of $a$'s},
+ − 1279
x label style={at={(1.05,-0.05)}},
+ − 1280
ylabel={regex size},
+ − 1281
enlargelimits=false,
+ − 1282
xtick={0,5,...,30},
+ − 1283
xmax=33,
+ − 1284
ymax= 40,
+ − 1285
ytick={0,10,...,40},
+ − 1286
scaled ticks=false,
+ − 1287
axis lines=left,
+ − 1288
width=5cm,
+ − 1289
height=4cm,
+ − 1290
legend entries={$(a + aa)^*$},
+ − 1291
legend pos=north west,
+ − 1292
legend cell align=left]
+ − 1293
\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
+ − 1294
\end{axis}
+ − 1295
\end{tikzpicture}
+ − 1296
\end{tabular}
+ − 1297
\end{center}
+ − 1298
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
+ − 1299
with our simplification
+ − 1300
rules very effectively.
+ − 1301
+ − 1302
+ − 1303
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
+ − 1304
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
+ − 1305
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
+ − 1306
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
+ − 1307
$f(x) = x * 2^x$.
+ − 1308
This means the bound we have will surge up at least
+ − 1309
tower-exponentially with a linear increase of the depth.
+ − 1310
For a regex of depth $n$, the bound
+ − 1311
would be approximately $4^n$.
+ − 1312
+ − 1313
Test data in the graphs from randomly generated regular expressions
+ − 1314
shows that the giant bounds are far from being hit.
+ − 1315
%a few sample regular experessions' derivatives
+ − 1316
%size change
+ − 1317
%TODO: giving regex1_size_change.data showing a few regexes' size changes
+ − 1318
%w;r;t the input characters number, where the size is usually cubic in terms of original size
+ − 1319
%a*, aa*, aaa*, .....
+ − 1320
%randomly generated regexes
+ − 1321
\begin{center}
+ − 1322
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
+ − 1323
\begin{tikzpicture}
+ − 1324
\begin{axis}[
+ − 1325
xlabel={number of $a$'s},
+ − 1326
x label style={at={(1.05,-0.05)}},
+ − 1327
ylabel={regex size},
+ − 1328
enlargelimits=false,
+ − 1329
xtick={0,5,...,30},
+ − 1330
xmax=33,
+ − 1331
ymax=1000,
+ − 1332
ytick={0,100,...,1000},
+ − 1333
scaled ticks=false,
+ − 1334
axis lines=left,
+ − 1335
width=5cm,
+ − 1336
height=4cm,
+ − 1337
legend entries={regex1},
+ − 1338
legend pos=north west,
+ − 1339
legend cell align=left]
+ − 1340
\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
+ − 1341
\end{axis}
+ − 1342
\end{tikzpicture}
+ − 1343
&
+ − 1344
\begin{tikzpicture}
+ − 1345
\begin{axis}[
+ − 1346
xlabel={$n$},
+ − 1347
x label style={at={(1.05,-0.05)}},
+ − 1348
%ylabel={time in secs},
+ − 1349
enlargelimits=false,
+ − 1350
xtick={0,5,...,30},
+ − 1351
xmax=33,
+ − 1352
ymax=1000,
+ − 1353
ytick={0,100,...,1000},
+ − 1354
scaled ticks=false,
+ − 1355
axis lines=left,
+ − 1356
width=5cm,
+ − 1357
height=4cm,
+ − 1358
legend entries={regex2},
+ − 1359
legend pos=north west,
+ − 1360
legend cell align=left]
+ − 1361
\addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
+ − 1362
\end{axis}
+ − 1363
\end{tikzpicture}
+ − 1364
&
+ − 1365
\begin{tikzpicture}
+ − 1366
\begin{axis}[
+ − 1367
xlabel={$n$},
+ − 1368
x label style={at={(1.05,-0.05)}},
+ − 1369
%ylabel={time in secs},
+ − 1370
enlargelimits=false,
+ − 1371
xtick={0,5,...,30},
+ − 1372
xmax=33,
+ − 1373
ymax=1000,
+ − 1374
ytick={0,100,...,1000},
+ − 1375
scaled ticks=false,
+ − 1376
axis lines=left,
+ − 1377
width=5cm,
+ − 1378
height=4cm,
+ − 1379
legend entries={regex3},
+ − 1380
legend pos=north west,
+ − 1381
legend cell align=left]
+ − 1382
\addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
+ − 1383
\end{axis}
+ − 1384
\end{tikzpicture}\\
+ − 1385
\multicolumn{3}{c}{Graphs: size change of 3 randomly generated regexes $w.r.t.$ input string length.}
+ − 1386
\end{tabular}
+ − 1387
\end{center}
+ − 1388
+ − 1389
+ − 1390
+ − 1391
+ − 1392
+ − 1393
\noindent
+ − 1394
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the
+ − 1395
original size.
+ − 1396
This suggests a link towrads "partial derivatives"
+ − 1397
introduced by Antimirov \cite{Antimirov95}.
+ − 1398
+ − 1399
\section{Antimirov's partial derivatives}
+ − 1400
The idea behind Antimirov's partial derivatives
+ − 1401
is to do derivatives in a similar way as suggested by Brzozowski,
+ − 1402
but maintain a set of regular expressions instead of a single one:
+ − 1403
+ − 1404
%TODO: antimirov proposition 3.1, needs completion
+ − 1405
\begin{center}
+ − 1406
\begin{tabular}{ccc}
+ − 1407
$\partial_x(a+b)$ & $=$ & $\partial_x(a) \cup \partial_x(b)$\\
+ − 1408
$\partial_x(\ONE)$ & $=$ & $\phi$
+ − 1409
\end{tabular}
+ − 1410
\end{center}
+ − 1411
+ − 1412
Rather than joining the calculated derivatives $\partial_x a$ and $\partial_x b$ together
+ − 1413
using the alternatives constructor, Antimirov cleverly chose to put them into
+ − 1414
a set instead. This breaks the terms in a derivative regular expression up,
+ − 1415
allowing us to understand what are the "atomic" components of it.
+ − 1416
For example, To compute what regular expression $x^*(xx + y)^*$'s
+ − 1417
derivative against $x$ is made of, one can do a partial derivative
+ − 1418
of it and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
+ − 1419
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
+ − 1420
To get all the "atomic" components of a regular expression's possible derivatives,
+ − 1421
there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
+ − 1422
whatever character is available at the head of the string inside the language of a
+ − 1423
regular expression, and gives back the character and the derivative regular expression
+ − 1424
as a pair (which he called "monomial"):
+ − 1425
\begin{center}
+ − 1426
\begin{tabular}{ccc}
+ − 1427
$\lf(\ONE)$ & $=$ & $\phi$\\
+ − 1428
$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
+ − 1429
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
+ − 1430
$\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
+ − 1431
\end{tabular}
+ − 1432
\end{center}
+ − 1433
%TODO: completion
+ − 1434
+ − 1435
There is a slight difference in the last three clauses compared
+ − 1436
with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular
+ − 1437
expression $r$ with every element inside $\textit{rset}$ to create a set of
+ − 1438
sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates
+ − 1439
on a set of monomials (which Antimirov called "linear form") and a regular
+ − 1440
expression, and returns a linear form:
+ − 1441
\begin{center}
+ − 1442
\begin{tabular}{ccc}
+ − 1443
$l \bigodot (\ZERO)$ & $=$ & $\phi$\\
+ − 1444
$l \bigodot (\ONE)$ & $=$ & $l$\\
+ − 1445
$\phi \bigodot t$ & $=$ & $\phi$\\
+ − 1446
$\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
+ − 1447
$\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
+ − 1448
$\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
+ − 1449
$\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
+ − 1450
$\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
+ − 1451
\end{tabular}
+ − 1452
\end{center}
+ − 1453
%TODO: completion
+ − 1454
+ − 1455
Some degree of simplification is applied when doing $\bigodot$, for example,
+ − 1456
$l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
+ − 1457
and $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
+ − 1458
$\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
+ − 1459
and so on.
+ − 1460
+ − 1461
With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regex $r$ with
+ − 1462
an iterative procedure:
+ − 1463
\begin{center}
+ − 1464
\begin{tabular}{llll}
+ − 1465
$\textit{while}$ & $(\Delta_i \neq \phi)$ & & \\
+ − 1466
& $\Delta_{i+1}$ & $ =$ & $\lf(\Delta_i) - \PD_i$ \\
+ − 1467
& $\PD_{i+1}$ & $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
+ − 1468
$\partial_{UNIV}(r)$ & $=$ & $\PD$ &
+ − 1469
\end{tabular}
+ − 1470
\end{center}
+ − 1471
+ − 1472
+ − 1473
$(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
+ − 1474
+ − 1475
+ − 1476
However, if we introduce them in our
+ − 1477
setting we would lose the POSIX property of our calculated values.
+ − 1478
A simple example for this would be the regex $(a + a\cdot b)\cdot(b\cdot c + c)$.
+ − 1479
If we split this regex up into $a\cdot(b\cdot c + c) + a\cdot b \cdot (b\cdot c + c)$, the lexer
+ − 1480
would give back $\Left(\Seq(\Char(a), \Left(\Char(b \cdot c))))$ instead of
+ − 1481
what we want: $\Seq(\Right(ab), \Right(c))$. Unless we can store the structural information
+ − 1482
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$
+ − 1483
occurs, and apply them in the right order once we get
+ − 1484
a result of the "aggressively simplified" regex, it would be impossible to still get a $\POSIX$ value.
+ − 1485
This is unlike the simplification we had before, where the rewriting rules
+ − 1486
such as $\ONE \cdot r \rightsquigarrow r$, under which our lexer will give the same value.
+ − 1487
We will discuss better
+ − 1488
bounds in the last section of this chapter.\\[-6.5mm]
+ − 1489
+ − 1490
+ − 1491
+ − 1492
+ − 1493
%----------------------------------------------------------------------------------------
+ − 1494
% SECTION ??
+ − 1495
%----------------------------------------------------------------------------------------
+ − 1496
+ − 1497
%-----------------------------------
+ − 1498
% SECTION syntactic equivalence under simp
+ − 1499
%-----------------------------------
+ − 1500
\section{Syntactic Equivalence Under $\simp$}
+ − 1501
We prove that minor differences can be annhilated
+ − 1502
by $\simp$.
+ − 1503
For example,
+ − 1504
\begin{center}
+ − 1505
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
+ − 1506
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
+ − 1507
\end{center}
+ − 1508
+ − 1509
+ − 1510
%----------------------------------------------------------------------------------------
+ − 1511
% SECTION ALTS CLOSED FORM
+ − 1512
%----------------------------------------------------------------------------------------
+ − 1513
\section{A Closed Form for \textit{ALTS}}
+ − 1514
Now we prove that $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
+ − 1515
+ − 1516
+ − 1517
There are a few key steps, one of these steps is
556
+ − 1518
532
+ − 1519
+ − 1520
+ − 1521
One might want to prove this by something a simple statement like:
+ − 1522
+ − 1523
For this to hold we want the $\textit{distinct}$ function to pick up
+ − 1524
the elements before and after derivatives correctly:
+ − 1525
$r \in rset \equiv (rder x r) \in (rder x rset)$.
+ − 1526
which essentially requires that the function $\backslash$ is an injective mapping.
+ − 1527
+ − 1528
Unfortunately the function $\backslash c$ is not an injective mapping.
+ − 1529
+ − 1530
\subsection{function $\backslash c$ is not injective (1-to-1)}
+ − 1531
\begin{center}
+ − 1532
The derivative $w.r.t$ character $c$ is not one-to-one.
+ − 1533
Formally,
+ − 1534
$\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
+ − 1535
\end{center}
+ − 1536
This property is trivially true for the
+ − 1537
character regex example:
+ − 1538
\begin{center}
+ − 1539
$r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
+ − 1540
\end{center}
+ − 1541
But apart from the cases where the derivative
+ − 1542
output is $\ZERO$, are there non-trivial results
+ − 1543
of derivatives which contain strings?
+ − 1544
The answer is yes.
+ − 1545
For example,
+ − 1546
\begin{center}
+ − 1547
Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
+ − 1548
where $a$ is not nullable.\\
+ − 1549
$r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
+ − 1550
$r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
+ − 1551
\end{center}
+ − 1552
We start with two syntactically different regexes,
+ − 1553
and end up with the same derivative result.
+ − 1554
This is not surprising as we have such
+ − 1555
equality as below in the style of Arden's lemma:\\
+ − 1556
\begin{center}
+ − 1557
$L(A^*B) = L(A\cdot A^* \cdot B + B)$
+ − 1558
\end{center}
+ − 1559
+ − 1560
%----------------------------------------------------------------------------------------
+ − 1561
% SECTION 4
+ − 1562
%----------------------------------------------------------------------------------------
+ − 1563
\section{A Bound for the Star Regular Expression}
+ − 1564
We have shown how to control the size of the sequence regular expression $r_1\cdot r_2$ using
+ − 1565
the "closed form" of $(r_1 \cdot r_2) \backslash s$ and then
+ − 1566
the property of the $\distinct$ function.
+ − 1567
Now we try to get a bound on $r^* \backslash s$ as well.
+ − 1568
Again, we first look at how a star's derivatives evolve, if they grow maximally:
+ − 1569
\begin{center}
+ − 1570
+ − 1571
$r^* \quad \longrightarrow_{\backslash c} \quad (r\backslash c) \cdot r^* \quad \longrightarrow_{\backslash c'} \quad
+ − 1572
r \backslash cc' \cdot r^* + r \backslash c' \cdot r^* \quad \longrightarrow_{\backslash c''} \quad
+ − 1573
(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'''}
+ − 1574
\quad \ldots$
+ − 1575
+ − 1576
\end{center}
+ − 1577
When we have a string $s = c :: c' :: c'' \ldots$ such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$,
+ − 1578
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
+ − 1579
the number of terms in $r^* \backslash s$ will grow exponentially, causing the size
+ − 1580
of the derivatives $r^* \backslash s$ to grow exponentially, even if we do not
+ − 1581
count the possible size explosions of $r \backslash c$ themselves.
+ − 1582
+ − 1583
Thanks to $\flts$ and $\distinctWith$, we are able to open up regexes like
+ − 1584
$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + (r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $
+ − 1585
into $\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'', r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
+ − 1586
and then de-duplicate terms of the form $r\backslash s' \cdot r^*$ ($s'$ being a substring of $s$).
+ − 1587
For this we define $\hflataux{\_}$ and $\hflat{\_}$, similar to $\sflataux{\_}$ and $\sflat{\_}$:
+ − 1588
%TODO: definitions of and \hflataux \hflat
+ − 1589
\begin{center}
+ − 1590
\begin{tabular}{ccc}
+ − 1591
$\hflataux{r_1 + r_2}$ & $=$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
+ − 1592
$\hflataux r$ & $=$ & $ [r]$
+ − 1593
\end{tabular}
+ − 1594
\end{center}
+ − 1595
+ − 1596
\begin{center}
+ − 1597
\begin{tabular}{ccc}
+ − 1598
$\hflat{r_1 + r_2}$ & $=$ & $\RALTS{\hflataux{r_1} @ \hflataux{r_2}}$\\
+ − 1599
$\hflat r$ & $=$ & $ r$
+ − 1600
\end{tabular}
+ − 1601
\end{center}s
+ − 1602
Again these definitions are tailor-made for dealing with alternatives that have
+ − 1603
originated from a star's derivatives, so we don't attempt to open up all possible
+ − 1604
regexes of the form $\RALTS{rs}$, where $\textit{rs}$ might not contain precisely 2
+ − 1605
elements.
+ − 1606
We give a predicate for such "star-created" regular expressions:
+ − 1607
\begin{center}
+ − 1608
\begin{tabular}{lcr}
+ − 1609
& & $\createdByStar{(\RSEQ{ra}{\RSTAR{rb}}) }$\\
+ − 1610
$\createdByStar{r_1} \land \createdByStar{r_2} $ & $ \Longrightarrow$ & $\createdByStar{(r_1 + r_2)}$
+ − 1611
\end{tabular}
+ − 1612
\end{center}
+ − 1613
+ − 1614
These definitions allows us the flexibility to talk about
+ − 1615
regular expressions in their most convenient format,
+ − 1616
for example, flattened out $\RALTS{[r_1, r_2, \ldots, r_n]} $
+ − 1617
instead of binary-nested: $((r_1 + r_2) + (r_3 + r_4)) + \ldots$.
+ − 1618
These definitions help express that certain classes of syntatically
+ − 1619
distinct regular expressions are actually the same under simplification.
+ − 1620
This is not entirely true for annotated regular expressions:
+ − 1621
%TODO: bsimp bders \neq bderssimp
+ − 1622
\begin{center}
+ − 1623
$(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
+ − 1624
\end{center}
+ − 1625
For bit-codes, the order in which simplification is applied
+ − 1626
might cause a difference in the location they are placed.
+ − 1627
If we want something like
+ − 1628
\begin{center}
+ − 1629
$\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
+ − 1630
\end{center}
+ − 1631
Some "canonicalization" procedure is required,
+ − 1632
which either pushes all the common bitcodes to nodes
+ − 1633
as senior as possible:
+ − 1634
\begin{center}
+ − 1635
$_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
+ − 1636
\end{center}
+ − 1637
or does the reverse. However bitcodes are not of interest if we are talking about
+ − 1638
the $\llbracket r \rrbracket$ size of a regex.
+ − 1639
Therefore for the ease and simplicity of producing a
+ − 1640
proof for a size bound, we are happy to restrict ourselves to
+ − 1641
unannotated regular expressions, and obtain such equalities as
+ − 1642
\begin{lemma}
+ − 1643
$\rsimp{r_1 + r_2} = \rsimp{\RALTS{\hflataux{r_1} @ \hflataux{r_2}}}$
+ − 1644
\end{lemma}
+ − 1645
+ − 1646
\begin{proof}
+ − 1647
By using the rewriting relation $\rightsquigarrow$
+ − 1648
\end{proof}
+ − 1649
%TODO: rsimp sflat
+ − 1650
And from this we obtain a proof that a star's derivative will be the same
+ − 1651
as if it had all its nested alternatives created during deriving being flattened out:
+ − 1652
For example,
+ − 1653
\begin{lemma}
+ − 1654
$\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
+ − 1655
\end{lemma}
+ − 1656
\begin{proof}
+ − 1657
By structural induction on $r$, where the induction rules are these of $\createdByStar{_}$.
+ − 1658
\end{proof}
+ − 1659
% The simplification of a flattened out regular expression, provided it comes
+ − 1660
%from the derivative of a star, is the same as the one nested.
+ − 1661
+ − 1662
+ − 1663
+ − 1664
+ − 1665
+ − 1666
+ − 1667
+ − 1668
+ − 1669
+ − 1670
One might wonder the actual bound rather than the loose bound we gave
+ − 1671
for the convenience of an easier proof.
+ − 1672
How much can the regex $r^* \backslash s$ grow?
+ − 1673
As earlier graphs have shown,
+ − 1674
%TODO: reference that graph where size grows quickly
+ − 1675
they can grow at a maximum speed
+ − 1676
exponential $w.r.t$ the number of characters,
+ − 1677
but will eventually level off when the string $s$ is long enough.
+ − 1678
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
+ − 1679
would still be slow.
+ − 1680
And unfortunately, we have concrete examples
+ − 1681
where such regexes grew exponentially large before levelling off:
+ − 1682
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
+ − 1683
(\underbrace{a \ldots a}_{\text{n a's}})^*$ will already have a maximum
+ − 1684
size that is exponential on the number $n$
+ − 1685
under our current simplification rules:
+ − 1686
%TODO: graph of a regex whose size increases exponentially.
+ − 1687
\begin{center}
+ − 1688
\begin{tikzpicture}
+ − 1689
\begin{axis}[
+ − 1690
height=0.5\textwidth,
+ − 1691
width=\textwidth,
+ − 1692
xlabel=number of a's,
+ − 1693
xtick={0,...,9},
+ − 1694
ylabel=maximum size,
+ − 1695
ymode=log,
+ − 1696
log basis y={2}
+ − 1697
]
+ − 1698
\addplot[mark=*,blue] table {re-chengsong.data};
+ − 1699
\end{axis}
+ − 1700
\end{tikzpicture}
+ − 1701
\end{center}
+ − 1702
+ − 1703
For convenience we use $(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
+ − 1704
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots +
+ − 1705
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
+ − 1706
The exponential size is triggered by that the regex
+ − 1707
$\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
+ − 1708
inside the $(\ldots) ^*$ having exponentially many
+ − 1709
different derivatives, despite those difference being minor.
+ − 1710
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
+ − 1711
will therefore contain the following terms (after flattening out all nested
+ − 1712
alternatives):
+ − 1713
\begin{center}
+ − 1714
$(\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}})^*)$\\
+ − 1715
$(1 \leq m' \leq m )$
+ − 1716
\end{center}
+ − 1717
These terms are distinct for $m' \leq L.C.M.(1, \ldots, n)$ (will be explained in appendix).
+ − 1718
With each new input character taking the derivative against the intermediate result, more and more such distinct
+ − 1719
terms will accumulate,
+ − 1720
until the length reaches $L.C.M.(1, \ldots, n)$.
+ − 1721
$\textit{distinctBy}$ will not be able to de-duplicate any two of these terms
+ − 1722
$(\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}})^*)^*$\\
+ − 1723
+ − 1724
$(\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}})^*)^*$\\
+ − 1725
where $m' \neq m''$ \\
+ − 1726
as they are slightly different.
+ − 1727
This means that with our current simplification methods,
+ − 1728
we will not be able to control the derivative so that
+ − 1729
$\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial %\leq O((\llbracket r\rrbacket)^c)$
+ − 1730
as there are already exponentially many terms.
+ − 1731
These terms are similar in the sense that the head of those terms
+ − 1732
are all consisted of sub-terms of the form:
+ − 1733
$(\underbrace{a \ldots a}_{\text{j a's}})\cdot (\underbrace{a \ldots a}_{\text{i a's}})^* $.
+ − 1734
For $\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
+ − 1735
$n * (n + 1) / 2$ such terms.
+ − 1736
For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
+ − 1737
can be described by 6 terms:
+ − 1738
$a^*$, $a\cdot (aa)^*$, $ (aa)^*$,
+ − 1739
$aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
+ − 1740
The total number of different "head terms", $n * (n + 1) / 2$,
+ − 1741
is proportional to the number of characters in the regex
+ − 1742
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
+ − 1743
This suggests a slightly different notion of size, which we call the
+ − 1744
alphabetic width:
+ − 1745
%TODO:
+ − 1746
(TODO: Alphabetic width def.)
+ − 1747
+ − 1748
+ − 1749
Antimirov\parencite{Antimirov95} has proven that
+ − 1750
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$.
+ − 1751
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
+ − 1752
created by doing derivatives of $r$ against all possible strings.
+ − 1753
If we can make sure that at any moment in our lexing algorithm our
+ − 1754
intermediate result hold at most one copy of each of the
+ − 1755
subterms then we can get the same bound as Antimirov's.
+ − 1756
This leads to the algorithm in the next chapter.
+ − 1757
+ − 1758
+ − 1759
+ − 1760
+ − 1761
+ − 1762
%----------------------------------------------------------------------------------------
+ − 1763
% SECTION 1
+ − 1764
%----------------------------------------------------------------------------------------
+ − 1765
+ − 1766
+ − 1767
%-----------------------------------
+ − 1768
% SUBSECTION 1
+ − 1769
%-----------------------------------
+ − 1770
\subsection{Syntactic Equivalence Under $\simp$}
+ − 1771
We prove that minor differences can be annhilated
+ − 1772
by $\simp$.
+ − 1773
For example,
+ − 1774
\begin{center}
+ − 1775
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) =
+ − 1776
\simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
+ − 1777
\end{center}
+ − 1778