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