ChengsongTanPhdThesis/Chapters/Finite.tex
author Chengsong
Fri, 03 Jun 2022 16:45:30 +0100
changeset 532 cc54ce075db5
child 543 b2bea5968b89
permissions -rwxr-xr-x
restructured
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     1
% Chapter Template
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     3
\chapter{Finiteness Bound} % Main chapter title
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     5
\label{Finite} 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     6
%  In Chapter 4 \ref{Chapter4} we give the second guarantee
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     7
%of our bitcoded algorithm, that is a finite bound on the size of any 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     8
%regex's derivatives. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
     9
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    10
In this chapter we give a guarantee in terms of time complexity:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    11
given a regular expression $r$, for any string $s$ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    12
our algorithm's internal data structure is finitely bounded.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    13
To obtain such a proof, we need to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    14
\begin{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    15
\item
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    16
Define an new datatype for regular expressions that makes it easy
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    17
to reason about the size of an annotated regular expression.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    18
\item
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    19
A set of equalities for this new datatype that enables one to
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    20
rewrite $\bderssimp{r_1 \cdot r_2}{s}$ and $\bderssimp{r^*}{s}$ etc.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    21
by their children regexes $r_1$, $r_2$, and $r$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    22
\item
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    23
Using those equalities to actually get those rewriting equations, which we call
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    24
"closed forms".
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    25
\item
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    26
Bound the closed forms, thereby bounding the original
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    27
$\blexersimp$'s internal data structures.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    28
\end{itemize}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    29
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    30
\section{the $\mathbf{r}$-rexp datatype and the size functions}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    31
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    32
We have a size function for bitcoded regular expressions, written
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    33
$\llbracket r\rrbracket$, which counts the number of nodes if we regard $r$ as a tree
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    34
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    35
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    36
\begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    37
$\llbracket \ACHAR{bs}{c} \rrbracket $ & $\dn$ & $1$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    38
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    39
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    40
(TODO: COMPLETE this defn and for $rs$)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    41
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    42
The size is based on a recursive function on the structure of the regex,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    43
not the bitcodes.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    44
Therefore we may as well talk about size of an annotated regular expression 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    45
in their un-annotated form:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    46
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    47
$\asize(a) = \size(\erase(a))$. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    48
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    49
(TODO: turn equals to roughly equals)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    50
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    51
But there is a minor nuisance here:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    52
the erase function actually messes with the structure of the regular expression:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    53
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    54
\begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    55
$\erase(\AALTS{bs}{[]} )$ & $\dn$ & $\ZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    56
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    57
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    58
(TODO: complete this definition with singleton r in alts)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    59
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    60
An alternative regular expression with an empty list of children
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    61
 is turned into an $\ZERO$ during the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    62
$\erase$ function, thereby changing the size and structure of the regex.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    63
These will likely be fixable if we really want to use plain $\rexp$s for dealing
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    64
with size, but we choose a more straightforward (or stupid) method by 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    65
defining a new datatype that is similar to plain $\rexp$s but can take
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    66
non-binary arguments for its alternative constructor,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    67
 which we call $\rrexp$ to denote
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    68
the difference between it and plain regular expressions. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    69
\[			\rrexp ::=   \RZERO \mid  \RONE
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    70
			 \mid  \RCHAR{c}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    71
			 \mid  \RSEQ{r_1}{r_2}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    72
			 \mid  \RALTS{rs}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    73
			 \mid \RSTAR{r}        
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    74
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    75
For $\rrexp$ we throw away the bitcodes on the annotated regular expressions, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    76
but keep everything else intact.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    77
It is similar to annotated regular expressions being $\erase$-ed, but with all its structure preserved
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    78
(the $\erase$ function unfortunately does not preserve structure in the case of empty and singleton
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    79
$\ALTS$).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    80
We denote the operation of erasing the bits and turning an annotated regular expression 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    81
into an $\rrexp{}$ as $\rerase{}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    82
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    83
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    84
$\rerase{\AZERO}$ & $=$ & $\RZERO$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    85
$\rerase{\ASEQ{bs}{r_1}{r_2}}$ & $=$ & $\RSEQ{\rerase{r_1}}{\rerase{r_2}}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    86
$\rerase{\AALTS{bs}{rs}}$ & $ =$ & $ \RALTS{rs}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    87
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    88
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    89
%TODO: FINISH definition of rerase
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    90
Similarly we could define the derivative  and simplification on 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    91
$\rrexp$, which would be identical to those we defined for plain $\rexp$s in chapter1, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    92
except that now they can operate on alternatives taking multiple arguments.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    93
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    94
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    95
\begin{tabular}{lcr}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    96
$\RALTS{rs} \backslash c$ & $=$ &  $\RALTS{map\; (\_ \backslash c) \;rs}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    97
(other clauses omitted)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    98
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
    99
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   100
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   101
Now that $\rrexp$s do not have bitcodes on them, we can do the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   102
duplicate removal without  an equivalence relation:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   103
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   104
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   105
$\rdistinct{r :: rs}{rset}$ & $=$ & $\textit{if}(r \in \textit{rset}) \; \textit{then} \; \rdistinct{rs}{rset}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   106
           			    &        & $\textit{else}\; r::\rdistinct{rs}{(rset \cup \{r\})}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   107
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   108
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   109
%TODO: definition of rsimp (maybe only the alternative clause)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   110
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   111
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   112
The reason why these definitions  mirror precisely the corresponding operations
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   113
on annotated regualar expressions is that we can calculate sizes more easily:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   114
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   115
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   116
$\rsize{\rerase a} = \asize a$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   117
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   118
This lemma says that the size of an r-erased regex is the same as the annotated regex.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   119
this does not hold for plain $\rexp$s due to their ways of how alternatives are represented.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   120
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   121
$\asize{\bsimp{a}} = \rsize{\rsimp{\rerase{a}}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   122
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   123
Putting these two together we get a property that allows us to estimate the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   124
size of an annotated regular expression derivative using its un-annotated counterpart:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   125
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   126
$\asize{\bderssimp{r}{s}} =  \rsize{\rderssimp{\rerase{r}}{s}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   127
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   128
Unless stated otherwise in this chapter all $\textit{rexp}$s without
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   129
 bitcodes are seen as $\rrexp$s.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   130
 We also use $r_1 + r_2$ and $\RALTS{[r_1, r_2]}$ interchageably
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   131
 as the former suits people's intuitive way of stating a binary alternative
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   132
 regular expression.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   133
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   134
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   135
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   136
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   137
%	SECTION ?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   138
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   139
\section{preparatory properties for getting a finiteness bound}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   140
Before we get to the proof that says the intermediate result of our lexer will
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   141
remain finitely bounded, which is an important efficiency/liveness guarantee,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   142
we shall first develop a few preparatory properties and definitions to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   143
make the process of proving that a breeze.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   144
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   145
We define rewriting relations for $\rrexp$s, which allows us to do the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   146
same trick as we did for the correctness proof,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   147
but this time we will have stronger equalities established.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   148
\subsection{"hrewrite" relation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   149
List of 1-step rewrite rules for regular expressions simplification without bitcodes:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   150
\begin{figure}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   151
\caption{the "h-rewrite" rules}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   152
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   153
r_1 \cdot \ZERO \rightarrow_h \ZERO \]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   154
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   155
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   156
\ZERO \cdot r_2 \rightarrow \ZERO 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   157
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   158
\end{figure}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   159
And we define an "grewrite" relation that works on lists:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   160
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   161
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   162
$ \ZERO :: rs$ & $\rightarrow_g$ & $rs$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   163
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   164
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   165
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   166
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   167
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   168
With these relations we prove
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   169
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   170
$rs \rightarrow rs'  \implies \RALTS{rs} \rightarrow \RALTS{rs'}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   171
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   172
which enables us to prove "closed forms" equalities such as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   173
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   174
$\sflat{(r_1 \cdot r_2) \backslash s} = \RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\suffix \; s \; r_1 ))}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   175
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   176
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   177
But the most involved part of the above lemma is proving the following:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   178
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   179
$\bsimp{\RALTS{rs @ \RALTS{rs_1} @ rs'}} = \bsimp{\RALTS{rs @rs_1 @ rs'}} $ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   180
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   181
which is needed in proving things like 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   182
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   183
$r \rightarrow_f r'  \implies \rsimp{r} \rightarrow \rsimp{r'}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   184
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   185
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   186
Fortunately this is provable by induction on the list $rs$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   187
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   188
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   189
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   190
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   191
%	SECTION 2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   192
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   193
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   194
\begin{theorem}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   195
For any regex $r$, $\exists N_r. \forall s. \; \llbracket{\bderssimp{r}{s}}\rrbracket \leq N_r$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   196
\end{theorem}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   197
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   198
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   199
\begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   200
We prove this by induction on $r$. The base cases for $\AZERO$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   201
$\AONE \textit{bs}$ and $\ACHAR \textit{bs} c$ are straightforward. The interesting case is
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   202
for sequences of the form $\ASEQ{bs}{r_1}{r_2}$. In this case our induction
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   203
hypotheses state $\exists N_1. \forall s. \; \llbracket \bderssimp{r}{s} \rrbracket \leq N_1$ and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   204
$\exists N_2. \forall s. \; \llbracket \bderssimp{r_2}{s} \rrbracket \leq N_2$. We can reason as follows
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   205
%
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   206
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   207
\begin{tabular}{lcll}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   208
& & $ \llbracket   \bderssimp{\ASEQ{bs}{r_1}{r_2} }{s} \rrbracket$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   209
& $ = $ & $\llbracket bsimp\,(\textit{ALTs}\;bs\;(\ASEQ{nil}{\bderssimp{ r_1}{s}}{ r_2} ::
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   210
    [\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]))\rrbracket $ & (1) \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   211
& $\leq$ &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   212
    $\llbracket\textit{\distinctWith}\,((\ASEQ{nil}{\bderssimp{r_1}{s}}{r_2}$) ::
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   213
    [$\bderssimp{ r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)])\,\approx\;{}\rrbracket + 1 $ & (2) \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   214
& $\leq$ & $\llbracket\ASEQ{bs}{\bderssimp{ r_1}{s}}{r_2}\rrbracket +
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   215
             \llbracket\textit{distinctWith}\,[\bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)]\,\approx\;{}\rrbracket + 1 $ & (3) \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   216
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 +
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   217
      \llbracket \distinctWith\,[ \bderssimp{r_2}{s'} \;|\; s' \in \textit{Suffix}( r_1, s)] \,\approx\;\rrbracket$ & (4)\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   218
& $\leq$ & $N_1 + \llbracket r_2\rrbracket + 2 + l_{N_{2}} * N_{2}$ & (5)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   219
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   220
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   221
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   222
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   223
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   224
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$).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   225
The reason why we could write the derivatives of a sequence this way is described in section 2.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   226
The term (2) is used to control (1). 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   227
That is because one can obtain an overall
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   228
smaller regex list
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   229
by flattening it and removing $\ZERO$s first before applying $\distinctWith$ on it.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   230
Section 3 is dedicated to its proof.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   231
In (3) we know that  $\llbracket \ASEQ{bs}{(\bderssimp{ r_1}{s}}{r_2}\rrbracket$ is 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   232
bounded by $N_1 + \llbracket{}r_2\rrbracket + 1$. In (5) we know the list comprehension contains only regular expressions of size smaller
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   233
than $N_2$. The list length after  $\distinctWith$ is bounded by a number, which we call $l_{N_2}$. It stands
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   234
for the number of distinct regular expressions smaller than $N_2$ (there can only be finitely many of them).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   235
We reason similarly for  $\STAR$.\medskip
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   236
\end{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   237
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   238
What guarantee does this bound give us?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   239
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   240
Whatever the regex is, it will not grow indefinitely.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   241
Take our previous example $(a + aa)^*$ as an example:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   242
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   243
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   244
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   245
\begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   246
    xlabel={number of $a$'s},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   247
    x label style={at={(1.05,-0.05)}},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   248
    ylabel={regex size},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   249
    enlargelimits=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   250
    xtick={0,5,...,30},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   251
    xmax=33,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   252
    ymax= 40,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   253
    ytick={0,10,...,40},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   254
    scaled ticks=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   255
    axis lines=left,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   256
    width=5cm,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   257
    height=4cm, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   258
    legend entries={$(a + aa)^*$},  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   259
    legend pos=north west,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   260
    legend cell align=left]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   261
\addplot[red,mark=*, mark options={fill=white}] table {a_aa_star.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   262
\end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   263
\end{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   264
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   265
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   266
We are able to limit the size of the regex $(a + aa)^*$'s derivatives
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   267
 with our simplification
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   268
rules very effectively.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   269
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   270
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   271
In our proof for the inductive case $r_1 \cdot r_2$, the dominant term in the bound
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   272
is $l_{N_2} * N_2$, where $N_2$ is the bound we have for $\llbracket \bderssimp{r_2}{s} \rrbracket$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   273
Given that $l_{N_2}$ is roughly the size $4^{N_2}$, the size bound $\llbracket \bderssimp{r_1 \cdot r_2}{s} \rrbracket$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   274
inflates the size bound of $\llbracket \bderssimp{r_2}{s} \rrbracket$ with the function
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   275
$f(x) = x * 2^x$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   276
This means the bound we have will surge up at least
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   277
tower-exponentially with a linear increase of the depth.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   278
For a regex of depth $n$, the bound
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   279
would be approximately $4^n$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   280
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   281
Test data in the graphs from randomly generated regular expressions
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   282
shows that the giant bounds are far from being hit.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   283
%a few sample regular experessions' derivatives
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   284
%size change
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   285
%TODO: giving regex1_size_change.data showing a few regexes' size changes 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   286
%w;r;t the input characters number, where the size is usually cubic in terms of original size
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   287
%a*, aa*, aaa*, .....
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   288
%randomly generated regexes
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   289
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   290
\begin{tabular}{@{}c@{\hspace{0mm}}c@{\hspace{0mm}}c@{}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   291
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   292
\begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   293
    xlabel={number of $a$'s},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   294
    x label style={at={(1.05,-0.05)}},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   295
    ylabel={regex size},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   296
    enlargelimits=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   297
    xtick={0,5,...,30},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   298
    xmax=33,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   299
    ymax=1000,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   300
    ytick={0,100,...,1000},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   301
    scaled ticks=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   302
    axis lines=left,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   303
    width=5cm,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   304
    height=4cm, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   305
    legend entries={regex1},  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   306
    legend pos=north west,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   307
    legend cell align=left]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   308
\addplot[red,mark=*, mark options={fill=white}] table {regex1_size_change.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   309
\end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   310
\end{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   311
  &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   312
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   313
\begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   314
    xlabel={$n$},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   315
    x label style={at={(1.05,-0.05)}},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   316
    %ylabel={time in secs},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   317
    enlargelimits=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   318
    xtick={0,5,...,30},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   319
    xmax=33,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   320
    ymax=1000,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   321
    ytick={0,100,...,1000},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   322
    scaled ticks=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   323
    axis lines=left,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   324
    width=5cm,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   325
    height=4cm, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   326
    legend entries={regex2},  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   327
    legend pos=north west,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   328
    legend cell align=left]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   329
\addplot[blue,mark=*, mark options={fill=white}] table {regex2_size_change.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   330
\end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   331
\end{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   332
  &
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   333
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   334
\begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   335
    xlabel={$n$},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   336
    x label style={at={(1.05,-0.05)}},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   337
    %ylabel={time in secs},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   338
    enlargelimits=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   339
    xtick={0,5,...,30},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   340
    xmax=33,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   341
    ymax=1000,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   342
    ytick={0,100,...,1000},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   343
    scaled ticks=false,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   344
    axis lines=left,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   345
    width=5cm,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   346
    height=4cm, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   347
    legend entries={regex3},  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   348
    legend pos=north west,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   349
    legend cell align=left]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   350
\addplot[cyan,mark=*, mark options={fill=white}] table {regex3_size_change.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   351
\end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   352
\end{tikzpicture}\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   353
\multicolumn{3}{c}{Graphs: size change of 3 randomly generated regexes $w.r.t.$ input string length.}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   354
\end{tabular}    
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   355
\end{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   356
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   357
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   358
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   359
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   360
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   361
\noindent
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   362
Most of the regex's sizes seem to stay within a polynomial bound $w.r.t$ the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   363
original size.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   364
This suggests a link towrads "partial derivatives"
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   365
 introduced by Antimirov \cite{Antimirov95}.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   366
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   367
 \section{Antimirov's partial derivatives}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   368
 The idea behind Antimirov's partial derivatives
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   369
is to do derivatives in a similar way as suggested by Brzozowski, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   370
but maintain a set of regular expressions instead of a single one:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   371
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   372
%TODO: antimirov proposition 3.1, needs completion
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   373
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   374
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   375
 $\partial_x(a+b)$ & $=$ & $\partial_x(a) \cup \partial_x(b)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   376
$\partial_x(\ONE)$ & $=$ & $\phi$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   377
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   378
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   379
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   380
Rather than joining the calculated derivatives $\partial_x a$ and $\partial_x b$ together
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   381
using the alternatives constructor, Antimirov cleverly chose to put them into
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   382
a set instead.  This breaks the terms in a derivative regular expression up, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   383
allowing us to understand what are the "atomic" components of it.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   384
For example, To compute what regular expression $x^*(xx + y)^*$'s 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   385
derivative against $x$ is made of, one can do a partial derivative
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   386
of it and get two singleton sets $\{x^* \cdot (xx + y)^*\}$ and $\{x \cdot (xx + y) ^* \}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   387
from $\partial_x(x^*) \cdot (xx + y) ^*$ and $\partial_x((xx + y)^*)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   388
To get all the "atomic" components of a regular expression's possible derivatives,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   389
there is a procedure Antimirov called $\textit{lf}$, short for "linear forms", that takes
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   390
whatever character is available at the head of the string inside the language of a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   391
regular expression, and gives back the character and the derivative regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   392
as a pair (which he called "monomial"):
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   393
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   394
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   395
 $\lf(\ONE)$ & $=$ & $\phi$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   396
$\lf(c)$ & $=$ & $\{(c, \ONE) \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   397
 $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   398
 $\lf(r^*)$ & $=$ & $\lf(r) \bigodot \lf(r^*)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   399
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   400
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   401
%TODO: completion
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   402
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   403
There is a slight difference in the last three clauses compared
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   404
with $\partial$: instead of a dot operator $ \textit{rset} \cdot r$ that attaches the regular 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   405
expression $r$ with every element inside $\textit{rset}$ to create a set of 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   406
sequence derivatives, it uses the "circle dot" operator $\bigodot$ which operates 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   407
on a set of monomials (which Antimirov called "linear form") and a regular 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   408
expression, and returns a linear form:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   409
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   410
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   411
 $l \bigodot (\ZERO)$ & $=$ & $\phi$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   412
 $l \bigodot (\ONE)$ & $=$ & $l$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   413
 $\phi \bigodot t$ & $=$ & $\phi$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   414
 $\{ (x, \ZERO) \} \bigodot t$ & $=$ & $\{(x,\ZERO) \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   415
 $\{ (x, \ONE) \} \bigodot t$ & $=$ & $\{(x,t) \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   416
  $\{ (x, p) \} \bigodot t$ & $=$ & $\{(x,p\cdot t) \}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   417
 $\lf(a+b)$ & $=$ & $\lf(a) \cup \lf(b)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   418
 $\lf(r^*)$ & $=$ & $\lf(r) \cdot \lf(r^*)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   419
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   420
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   421
%TODO: completion
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   422
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   423
 Some degree of simplification is applied when doing $\bigodot$, for example,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   424
 $l \bigodot (\ZERO) = \phi$ corresponds to $r \cdot \ZERO \rightsquigarrow \ZERO$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   425
 and  $l \bigodot (\ONE) = l$ to $l \cdot \ONE \rightsquigarrow l$, and
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   426
  $\{ (x, \ZERO) \} \bigodot t = \{(x,\ZERO) \}$ to $\ZERO \cdot x \rightsquigarrow \ZERO$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   427
  and so on.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   428
  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   429
  With the function $\lf$ one can compute all possible partial derivatives $\partial_{UNIV}(r)$ of a regex $r$ with 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   430
  an iterative procedure:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   431
   \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   432
 \begin{tabular}{llll}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   433
$\textit{while}$ & $(\Delta_i \neq \phi)$  &                &          \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   434
 		       &  $\Delta_{i+1}$           &        $ =$ & $\lf(\Delta_i) - \PD_i$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   435
		       &  $\PD_{i+1}$              &         $ =$ & $\Delta_{i+1} \cup \PD_i$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   436
$\partial_{UNIV}(r)$ & $=$ & $\PD$ &		     
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   437
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   438
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   439
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   440
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   441
 $(r_1 + r_2) \cdot r_3 \longrightarrow (r_1 \cdot r_3) + (r_2 \cdot r_3)$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   442
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   443
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   444
However, if we introduce them in our
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   445
setting we would lose the POSIX property of our calculated values. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   446
A simple example for this would be the regex $(a + a\cdot b)\cdot(b\cdot c + c)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   447
If we split this regex up into $a\cdot(b\cdot c + c) + a\cdot b \cdot (b\cdot c + c)$, the lexer 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   448
would give back $\Left(\Seq(\Char(a), \Left(\Char(b \cdot c))))$ instead of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   449
what we want: $\Seq(\Right(ab), \Right(c))$. Unless we can store the structural information
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   450
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$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   451
occurs, and apply them in the right order once we get 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   452
a result of the "aggressively simplified" regex, it would be impossible to still get a $\POSIX$ value.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   453
This is unlike the simplification we had before, where the rewriting rules 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   454
such  as $\ONE \cdot r \rightsquigarrow r$, under which our lexer will give the same value.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   455
We will discuss better
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   456
bounds in the last section of this chapter.\\[-6.5mm]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   457
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   458
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   459
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   460
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   461
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   462
%	SECTION ??
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   463
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   464
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   465
\section{"Closed Forms" of regular expressions' derivatives w.r.t strings}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   466
To embark on getting the "closed forms" of regexes, we first
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   467
define a few auxiliary definitions to make expressing them smoothly.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   468
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   469
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   470
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   471
 $\sflataux{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   472
$\sflataux{\AALTS{ }{[]}}$ & $ = $ & $ []$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   473
$\sflataux r$ & $=$ & $ [r]$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   474
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   475
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   476
The intuition behind $\sflataux{\_}$ is to break up nested regexes 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   477
of the $(\ldots((r_1 + r_2) + r_3) + \ldots )$(left-associated) shape
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   478
into a more "balanced" list: $\AALTS{\_}{[r_1,\, r_2 ,\, r_3, \ldots]}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   479
It will return the singleton list $[r]$ otherwise.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   480
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   481
$\sflat{\_}$ works the same  as $\sflataux{\_}$, except that it keeps
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   482
the output type a regular expression, not a list:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   483
 \begin{center} 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   484
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   485
 $\sflat{\AALTS{ }{r :: rs}}$ & $=$ & $\sflataux{r} @ rs$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   486
$\sflat{\AALTS{ }{[]}}$ & $ = $ & $ \AALTS{ }{[]}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   487
$\sflat r$ & $=$ & $ [r]$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   488
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   489
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   490
$\sflataux{\_}$  and $\sflat{\_}$ is only recursive in terms of the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   491
 first element of the list of children of
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   492
an alternative regex ($\AALTS{ }{rs}$), and is purposefully built for  dealing with the regular expression
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   493
$r_1 \cdot r_2 \backslash s$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   494
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   495
With $\sflat{\_}$ and $\sflataux{\_}$ we make 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   496
precise what  "closed forms" we have for the sequence derivatives and their simplifications,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   497
in other words, how can we construct $(r_1 \cdot r_2) \backslash s$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   498
and $\bderssimp{(r_1\cdot r_2)}{s}$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   499
if we are only allowed to use a combination of $r_1 \backslash s'$ ($\bderssimp{r_1}{s'}$)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   500
and  $r_2 \backslash s'$ ($\bderssimp{r_2}{s'}$), where $s'$  ranges over 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   501
the substring of $s$?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   502
First let's look at a series of derivatives steps on a sequence 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   503
regular expression,  (assuming) that each time the first
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   504
component of the sequence is always nullable):
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   505
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   506
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   507
$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$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   508
$((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
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   509
 \ldots$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   510
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   511
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   512
%TODO: cite indian paper
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   513
Indianpaper have  come up with a slightly more formal way of putting the above process:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   514
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   515
$r_1 \cdot r_2 \backslash (c_1 :: c_2 ::\ldots c_n) \myequiv r_1 \backslash (c_1 :: c_2:: \ldots c_n) +
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   516
\delta(\nullable(r_1 \backslash (c_1 :: c_2 \ldots c_{n-1}) ), r_2 \backslash (c_n)) + \ldots
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   517
+ \delta (\nullable(r_1), r_2\backslash (c_1 :: c_2 ::\ldots c_n))$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   518
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   519
where  $\delta(b, r)$ will produce $r$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   520
if $b$ evaluates to true, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   521
and $\ZERO$ otherwise.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   522
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   523
 But the $\myequiv$ sign in the above equation means language equivalence instead of syntactical
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   524
 equivalence. To make this intuition useful 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   525
 for a formal proof, we need something
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   526
stronger than language equivalence.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   527
With the help of $\sflat{\_}$ we can state the equation in Indianpaper
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   528
more rigorously:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   529
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   530
$\sflat{(r_1 \cdot r_2) \backslash s} = \RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   531
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   532
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   533
The function $\vsuf{\_}{\_}$ is defined recursively on the structure of the string:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   534
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   535
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   536
\begin{tabular}{lcl}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   537
$\vsuf{[]}{\_} $ & $=$ &  $[]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   538
$\vsuf{c::cs}{r_1}$ & $ =$ & $ \textit{if} (\rnullable{r_1}) \textit{then} \; (\vsuf{cs}{(\rder{c}{r_1})}) @ [c :: cs]$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   539
                                     && $\textit{else} \; (\vsuf{cs}{(\rder{c}{r_1}) })  $
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   540
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   541
\end{center}                   
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   542
It takes into account which prefixes $s'$ of $s$ would make $r \backslash s'$ nullable,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   543
and keep a list of suffixes $s''$ such that $s' @ s'' = s$. The list is sorted in
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   544
the order $r_2\backslash s''$ appears in $(r_1\cdot r_2)\backslash s$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   545
In essence, $\vsuf{\_}{\_}$ is doing a "virtual derivative" of $r_1 \cdot r_2$, but instead of producing 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   546
the entire result of  $(r_1 \cdot r_2) \backslash s$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   547
it only stores all the $s''$ such that $r_2 \backslash s''$  are occurring terms in $(r_1\cdot r_2)\backslash s$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   548
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   549
With this we can also add simplifications to both sides and get
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   550
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   551
$\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}))}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   552
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   553
Together with the idempotency property of $\rsimp$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   554
%TODO: state the idempotency property of rsimp
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   555
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   556
$\rsimp{r} = \rsimp{\rsimp{r}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   557
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   558
it is possible to convert the above lemma to obtain a "closed form"
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   559
for  derivatives nested with simplification:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   560
\begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   561
$\rderssimp{(r_1 \cdot r_2)}{s} = \rsimp{\AALTS{[[]}{ (r_1 \backslash s) \cdot r_2 :: (\map (r_2 \backslash \_) (\vsuf{s}{r_1}))}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   562
\end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   563
And now the reason we have (1) in section 1 is clear:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   564
$\rsize{\rsimp{\RALTS{ (r_1 \backslash s) \cdot r_2 :: (\map \;(r_2 \backslash \_) \; (\vsuf{s}{r_1}))}}}$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   565
is equal to $\rsize{\rsimp{\RALTS{ ((r_1 \backslash s) \cdot r_2 :: (\map \; (r_2 \backslash \_) \; (\textit{Suffix}(r1, s))))}}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   566
    as $\vsuf{s}{r_1}$ is one way of expressing the list $\textit{Suffix}( r_1, s)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   567
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   568
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   569
%	SECTION 3
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   570
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   571
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   572
\section{interaction between $\distinctWith$ and $\flts$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   573
Note that it is not immediately obvious that 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   574
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   575
$\llbracket \distinctWith (\flts \textit{rs}) = \phi \rrbracket   \leq \llbracket \distinctWith \textit{rs} = \phi \rrbracket  $.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   576
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   577
The intuition is that if we remove duplicates from the $\textit{LHS}$, at least the same amount of 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   578
duplicates will be removed from the list $\textit{rs}$ in the $\textit{RHS}$. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   579
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   580
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   581
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   582
%	SECTION syntactic equivalence under simp
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   583
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   584
\section{Syntactic Equivalence Under $\simp$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   585
We prove that minor differences can be annhilated
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   586
by $\simp$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   587
For example,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   588
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   589
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) = 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   590
 \simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   591
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   592
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   593
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   594
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   595
%	SECTION ALTS CLOSED FORM
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   596
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   597
\section{A Closed Form for \textit{ALTS}}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   598
Now we prove that  $rsimp (rders\_simp (RALTS rs) s) = rsimp (RALTS (map (\lambda r. rders\_simp r s) rs))$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   599
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   600
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   601
There are a few key steps, one of these steps is
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   602
\[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   603
rsimp (rsimp\_ALTs (map (rder x) (rdistinct (rflts (map (rsimp \circ (\lambda r. rders\_simp r xs)) rs)) {})))
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   604
= rsimp (rsimp\_ALTs (rdistinct (map (rder x) (rflts (map (rsimp \circ (\lambda r. rders\_simp r xs)) rs))) {}))
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   605
\]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   606
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   607
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   608
One might want to prove this by something a simple statement like: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   609
$map (rder x) (rdistinct rs rset) = rdistinct (map (rder x) rs) (rder x) ` rset)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   610
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   611
For this to hold we want the $\textit{distinct}$ function to pick up
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   612
the elements before and after derivatives correctly:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   613
$r \in rset \equiv (rder x r) \in (rder x rset)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   614
which essentially requires that the function $\backslash$ is an injective mapping.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   615
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   616
Unfortunately the function $\backslash c$ is not an injective mapping.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   617
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   618
\subsection{function $\backslash c$ is not injective (1-to-1)}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   619
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   620
The derivative $w.r.t$ character $c$ is not one-to-one.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   621
Formally,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   622
	$\exists r_1 \;r_2. r_1 \neq r_2 \mathit{and} r_1 \backslash c = r_2 \backslash c$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   623
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   624
This property is trivially true for the
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   625
character regex example:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   626
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   627
	$r_1 = e; \; r_2 = d;\; r_1 \backslash c = \ZERO = r_2 \backslash c$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   628
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   629
But apart from the cases where the derivative
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   630
output is $\ZERO$, are there non-trivial results
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   631
of derivatives which contain strings?
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   632
The answer is yes.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   633
For example,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   634
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   635
	Let $r_1 = a^*b\;\quad r_2 = (a\cdot a^*)\cdot b + b$.\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   636
	where $a$ is not nullable.\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   637
	$r_1 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   638
	$r_2 \backslash c = ((a \backslash c)\cdot a^*)\cdot c + b \backslash c$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   639
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   640
We start with two syntactically different regexes,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   641
and end up with the same derivative result.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   642
This is not surprising as we have such 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   643
equality as below in the style of Arden's lemma:\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   644
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   645
	$L(A^*B) = L(A\cdot A^* \cdot B + B)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   646
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   647
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   648
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   649
%	SECTION 4
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   650
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   651
\section{A Bound for the Star Regular Expression}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   652
We have shown how to control the size of the sequence regular expression $r_1\cdot r_2$ using
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   653
the "closed form" of $(r_1 \cdot r_2) \backslash s$ and then 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   654
the property of the $\distinct$ function.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   655
Now we try to get a bound on $r^* \backslash s$ as well.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   656
Again, we first look at how a star's derivatives evolve, if they grow maximally: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   657
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   658
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   659
$r^* \quad \longrightarrow_{\backslash c}  \quad   (r\backslash c)  \cdot  r^* \quad \longrightarrow_{\backslash c'}  \quad
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   660
r \backslash cc'  \cdot r^* + r \backslash c' \cdot r^*  \quad \longrightarrow_{\backslash c''} \quad 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   661
(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'''}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   662
\quad \ldots$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   663
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   664
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   665
When we have a string $s = c :: c' :: c'' \ldots$  such that $r \backslash c$, $r \backslash cc'$, $r \backslash c'$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   666
$r \backslash cc'c''$, $r \backslash c'c''$, $r\backslash c''$ etc. are all nullable,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   667
the number of terms in $r^* \backslash s$ will grow exponentially, causing the size
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   668
of the derivatives $r^* \backslash s$ to grow exponentially, even if we do not 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   669
count the possible size explosions of $r \backslash c$ themselves.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   670
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   671
Thanks to $\flts$ and $\distinctWith$, we are able to open up regexes like
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   672
$(r_1 \backslash cc'c'' \cdot r^* + r \backslash c'') + (r \backslash c'c'' \cdot r^* + r \backslash c'' \cdot r^*) $ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   673
into $\RALTS{[r_1 \backslash cc'c'' \cdot r^*, r \backslash c'', r \backslash c'c'' \cdot r^*, r \backslash c'' \cdot r^*]}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   674
and then de-duplicate terms of the form $r\backslash s' \cdot r^*$ ($s'$ being a substring of $s$).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   675
For this we define $\hflataux{\_}$ and $\hflat{\_}$, similar to $\sflataux{\_}$ and $\sflat{\_}$:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   676
%TODO: definitions of  and \hflataux \hflat
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   677
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   678
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   679
 $\hflataux{r_1 + r_2}$ & $=$ & $\hflataux{r_1} @ \hflataux{r_2}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   680
$\hflataux r$ & $=$ & $ [r]$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   681
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   682
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   683
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   684
 \begin{center}  
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   685
 \begin{tabular}{ccc}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   686
 $\hflat{r_1 + r_2}$ & $=$ & $\RALTS{\hflataux{r_1} @ \hflataux{r_2}}$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   687
$\hflat r$ & $=$ & $ r$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   688
\end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   689
\end{center}s
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   690
Again these definitions are tailor-made for dealing with alternatives that have
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   691
originated from a star's derivatives, so we don't attempt to open up all possible 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   692
regexes of the form $\RALTS{rs}$, where $\textit{rs}$ might not contain precisely 2
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   693
elements.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   694
We give a predicate for such "star-created" regular expressions:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   695
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   696
\begin{tabular}{lcr}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   697
         &    &       $\createdByStar{(\RSEQ{ra}{\RSTAR{rb}}) }$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   698
 $\createdByStar{r_1} \land \createdByStar{r_2} $ & $ \Longrightarrow$ & $\createdByStar{(r_1 + r_2)}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   699
 \end{tabular}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   700
 \end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   701
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   702
 These definitions allows us the flexibility to talk about 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   703
 regular expressions in their most convenient format,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   704
 for example, flattened out $\RALTS{[r_1, r_2, \ldots, r_n]} $
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   705
 instead of binary-nested: $((r_1 + r_2) + (r_3 + r_4)) + \ldots$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   706
 These definitions help express that certain classes of syntatically 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   707
 distinct regular expressions are actually the same under simplification.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   708
 This is not entirely true for annotated regular expressions: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   709
 %TODO: bsimp bders \neq bderssimp
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   710
 \begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   711
 $(1+ (c\cdot \ASEQ{bs}{c^*}{c} ))$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   712
 \end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   713
 For bit-codes, the order in which simplification is applied
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   714
 might cause a difference in the location they are placed.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   715
 If we want something like
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   716
 \begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   717
 $\bderssimp{r}{s} \myequiv \bsimp{\bders{r}{s}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   718
 \end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   719
 Some "canonicalization" procedure is required,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   720
 which either pushes all the common bitcodes to nodes
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   721
  as senior as possible:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   722
  \begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   723
  $_{bs}(_{bs_1 @ bs'}r_1 + _{bs_1 @ bs''}r_2) \rightarrow _{bs @ bs_1}(_{bs'}r_1 + _{bs''}r_2) $
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   724
  \end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   725
 or does the reverse. However bitcodes are not of interest if we are talking about
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   726
 the $\llbracket r \rrbracket$ size of a regex.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   727
 Therefore for the ease and simplicity of producing a
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   728
 proof for a size bound, we are happy to restrict ourselves to 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   729
 unannotated regular expressions, and obtain such equalities as
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   730
 \begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   731
 $\rsimp{r_1 + r_2} = \rsimp{\RALTS{\hflataux{r_1} @ \hflataux{r_2}}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   732
 \end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   733
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   734
 \begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   735
 By using the rewriting relation $\rightsquigarrow$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   736
 \end{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   737
 %TODO: rsimp sflat
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   738
And from this we obtain a proof that a star's derivative will be the same
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   739
as if it had all its nested alternatives created during deriving being flattened out:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   740
 For example,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   741
 \begin{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   742
 $\createdByStar{r} \implies \rsimp{r} = \rsimp{\RALTS{\hflataux{r}}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   743
 \end{lemma}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   744
 \begin{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   745
 By structural induction on $r$, where the induction rules are these of $\createdByStar{_}$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   746
 \end{proof}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   747
% The simplification of a flattened out regular expression, provided it comes
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   748
%from the derivative of a star, is the same as the one nested.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   749
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   750
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   751
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   752
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   753
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   754
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   755
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   756
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   757
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   758
One might wonder the actual bound rather than the loose bound we gave
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   759
for the convenience of an easier proof.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   760
How much can the regex $r^* \backslash s$ grow? 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   761
As  earlier graphs have shown,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   762
%TODO: reference that graph where size grows quickly
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   763
 they can grow at a maximum speed
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   764
  exponential $w.r.t$ the number of characters, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   765
but will eventually level off when the string $s$ is long enough.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   766
If they grow to a size exponential $w.r.t$ the original regex, our algorithm
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   767
would still be slow.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   768
And unfortunately, we have concrete examples
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   769
where such regexes grew exponentially large before levelling off:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   770
$(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots + 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   771
(\underbrace{a \ldots a}_{\text{n a's}})^*$ will already have a maximum
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   772
 size that is  exponential on the number $n$ 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   773
under our current simplification rules:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   774
%TODO: graph of a regex whose size increases exponentially.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   775
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   776
\begin{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   777
    \begin{axis}[
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   778
        height=0.5\textwidth,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   779
        width=\textwidth,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   780
        xlabel=number of a's,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   781
        xtick={0,...,9},
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   782
        ylabel=maximum size,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   783
        ymode=log,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   784
       log basis y={2}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   785
]
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   786
        \addplot[mark=*,blue] table {re-chengsong.data};
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   787
    \end{axis}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   788
\end{tikzpicture}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   789
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   790
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   791
For convenience we use $(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   792
to express $(a ^ * + (aa) ^ * + (aaa) ^ * + \ldots + 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   793
(\underbrace{a \ldots a}_{\text{n a's}})^*$ in the below discussion.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   794
The exponential size is triggered by that the regex
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   795
$\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   796
inside the $(\ldots) ^*$ having exponentially many
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   797
different derivatives, despite those difference being minor.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   798
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*\backslash \underbrace{a \ldots a}_{\text{m a's}}$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   799
will therefore contain the following terms (after flattening out all nested 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   800
alternatives):
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   801
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   802
$(\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}})^*)$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   803
$(1 \leq m' \leq m )$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   804
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   805
These terms are distinct for $m' \leq L.C.M.(1, \ldots, n)$ (will be explained in appendix).
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   806
 With each new input character taking the derivative against the intermediate result, more and more such distinct
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   807
 terms will accumulate, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   808
until the length reaches $L.C.M.(1, \ldots, n)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   809
$\textit{distinctBy}$ will not be able to de-duplicate any two of these terms 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   810
$(\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}})^*)^*$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   811
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   812
$(\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}})^*)^*$\\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   813
 where $m' \neq m''$ \\
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   814
 as they are slightly different.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   815
 This means that with our current simplification methods,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   816
 we will not be able to control the derivative so that
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   817
 $\llbracket \bderssimp{r}{s} \rrbracket$ stays polynomial %\leq O((\llbracket r\rrbacket)^c)$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   818
 as there are already exponentially many terms.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   819
 These terms are similar in the sense that the head of those terms
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   820
 are all consisted of sub-terms of the form: 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   821
 $(\underbrace{a \ldots a}_{\text{j a's}})\cdot  (\underbrace{a \ldots a}_{\text{i a's}})^* $.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   822
 For  $\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*$, there will be at most
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   823
 $n * (n + 1) / 2$ such terms. 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   824
 For example, $(a^* + (aa)^* + (aaa)^*) ^*$'s derivatives
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   825
 can be described by 6 terms:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   826
 $a^*$, $a\cdot (aa)^*$, $ (aa)^*$, 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   827
 $aa \cdot (aaa)^*$, $a \cdot (aaa)^*$, and $(aaa)^*$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   828
The total number of different "head terms",  $n * (n + 1) / 2$,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   829
 is proportional to the number of characters in the regex 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   830
$(\oplus_{i=1}^{n} (\underbrace{a \ldots a}_{\text{i a's}})^*)^*$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   831
This suggests a slightly different notion of size, which we call the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   832
alphabetic width:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   833
%TODO:
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   834
(TODO: Alphabetic width def.)
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   835
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   836
 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   837
Antimirov\parencite{Antimirov95} has proven that 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   838
$\textit{PDER}_{UNIV}(r) \leq \textit{awidth}(r)$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   839
where $\textit{PDER}_{UNIV}(r)$ is a set of all possible subterms
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   840
created by doing derivatives of $r$ against all possible strings.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   841
If we can make sure that at any moment in our lexing algorithm our 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   842
intermediate result hold at most one copy of each of the 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   843
subterms then we can get the same bound as Antimirov's.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   844
This leads to the algorithm in the next chapter.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   845
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   846
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   847
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   848
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   849
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   850
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   851
%	SECTION 1
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   852
%----------------------------------------------------------------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   853
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   854
\section{Idempotency of $\simp$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   855
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   856
\begin{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   857
	\simp \;r = \simp\; \simp \; r 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   858
\end{equation}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   859
This property means we do not have to repeatedly
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   860
apply simplification in each step, which justifies
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   861
our definition of $\blexersimp$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   862
It will also be useful in future proofs where properties such as 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   863
closed forms are needed.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   864
The proof is by structural induction on $r$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   865
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   866
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   867
%	SUBSECTION 1
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   868
%-----------------------------------
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   869
\subsection{Syntactic Equivalence Under $\simp$}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   870
We prove that minor differences can be annhilated
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   871
by $\simp$.
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   872
For example,
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   873
\begin{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   874
$\simp \;(\simpALTs\; (\map \;(\_\backslash \; x)\; (\distinct \; \mathit{rs}\; \phi))) = 
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   875
 \simp \;(\simpALTs \;(\distinct \;(\map \;(\_ \backslash\; x) \; \mathit{rs}) \; \phi))$
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   876
\end{center}
cc54ce075db5 restructured
Chengsong
parents:
diff changeset
   877