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